using generic procedures

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 3
Reputation: uma625010 is an unknown quantity at this point 
Solved Threads: 0
uma625010 uma625010 is offline Offline
Newbie Poster

using generic procedures

 
0
  #1
Oct 4th, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: using generic procedures

 
0
  #2
Oct 4th, 2008
  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 ??
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: uma625010 is an unknown quantity at this point 
Solved Threads: 0
uma625010 uma625010 is offline Offline
Newbie Poster

Re: using generic procedures

 
0
  #3
Oct 5th, 2008
Originally Posted by manal View Post
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.
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC