Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Int16, y As Int16
        x = CInt(TextBox1.Text)
        y = CInt(TextBox2.Text)
        add(10, 20)
    End Sub


    Private Sub add(Of T)(ByVal x As T, ByVal y As T)
        Dim res As T
        res = x + y
        MsgBox(res)
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim x As Double, y As Double
        x = CDbl(TextBox1.Text)
        y = CDbl(TextBox2.Text)

        add(4.5, 5.6)
    End Sub

This is the code i used but there is an error " Operator + not defined for datatype 'T' and 'T' ". what is wrong in this. thanks in advance

Private Sub add(Of T)(ByVal x As T, ByVal y As T)
        Dim res As T
        res = x + y
        MsgBox(res)
    End Sub

what type T ??

what type T ??

T is to accept any data type that we pass as the argument. that is the main advantage of generics in .net. the following code works fine, but doesnot allow the + opertor.

Private Sub add(Of T)(ByVal x As T, ByVal y As T)
        'Dim res As T
        'res = x + y
        MsgBox("The values are " & x.ToString & ", " & y.ToString)


    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.