0
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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by uma625010
0
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 ??

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.