943,901 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 511
  • VB.NET RSS
Oct 4th, 2008
0

using generic procedures

Expand Post »
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. Dim x As Int16, y As Int16
  4. x = CInt(TextBox1.Text)
  5. y = CInt(TextBox2.Text)
  6. add(10, 20)
  7. End Sub
  8.  
  9.  
  10. Private Sub add(Of T)(ByVal x As T, ByVal y As T)
  11. Dim res As T
  12. res = x + y
  13. MsgBox(res)
  14. End Sub
  15.  
  16. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  17. Dim x As Double, y As Double
  18. x = CDbl(TextBox1.Text)
  19. y = CDbl(TextBox2.Text)
  20.  
  21. add(4.5, 5.6)
  22. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uma625010 is offline Offline
3 posts
since May 2008
Oct 4th, 2008
0

Re: using generic procedures

Quote ...
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub add(Of T)(ByVal x As T, ByVal y As T)
  2. Dim res As T
  3. res = x + y
  4. MsgBox(res)
  5. End Sub
what type T ??
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Oct 5th, 2008
0

Re: using generic procedures

Click to Expand / Collapse  Quote originally posted by manal ...
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.
VB.NET Syntax (Toggle Plain Text)
  1.  
  2.  
  3. Private Sub add(Of T)(ByVal x As T, ByVal y As T)
  4. 'Dim res As T
  5. 'res = x + y
  6. MsgBox("The values are " & x.ToString & ", " & y.ToString)
  7.  
  8.  
  9. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uma625010 is offline Offline
3 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: help in my linkin....
Next Thread in VB.NET Forum Timeline: help with time.....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC