Last post May 27, 2010 09:06 AM by atconway
Apr 24, 2010 01:19 AM|shyam_oec|LINK
in VB.NET when i define a subroutine (or function) , while declaring arguments , a list comes out. Having values ByVal,ByRef,Of,Optional, and ParamArray. Of these five lists , four are better understood except "Of". I searched and found that "Of" is used
for Generics .Generic Class or Generic Method.
But i did'nt find any procedure that how to create and use Generic Method in VB.NET. I found same in VB.C# language.
Can you tell how to create and use Generic Method in VB.NET.
Apr 24, 2010 06:02 AM|compguy2100|LINK
Here's the basic syntax of how you could create (and use) a Generic Class containing a method in VB.NET:
Class Generic(Of T, U)
Public V1 As T
Public V2 As U
Public Sub New(ByVal _V1 As T, _V2 As U)
V1 = _V1
V2 = _V2
How to use (or consume) an instance of this Generic type:
Dim gen As New Generic(Of String, String) ("Hello, ", "World!")
Response.Write(gen.V1 + gen.V2)
' Output: Hello, World
Depending on how the consuming code uses the Gen class, T and U could be a string, an integer, a custom class, or any combination of these... When you consume a generic type, you must specify the types for any generics used - in this case, we specified
2 strings when we created the new instance of this generic class (Of String, String). But, we could have also specified integers - like this:
Dim gen As New Generic(Of Integer, Integer) ( 2, 1)
' Output: 3
Apr 25, 2010 11:30 PM|shyam_oec|LINK
Ok ,but first i only want to concentrate on Generic Methods only. For that i wrote this code
Public Sub GenericMethodTestForSum(Of VarType)(ByVal number1 As VarType, ByVal number2 As VarType)
Dim x As VarType = number1 + number2 'error
I am tyying to call this method in pageLoad event. But in GenericMethodTestForSum(), i get compile time error ,that "Operator + is not defined for types VarType and VarType.
So how to define "+" operator here?
Apr 27, 2010 03:02 AM|shyam_oec|LINK
What happend? Is there no any answer to my question ? Or my question is absurd?
Apr 27, 2010 09:03 AM|pauldaniel|LINK
Refer this links
Apr 27, 2010 04:31 PM|atconway|LINK
Actually I covered this is detail in the link below! It should help you out in understanding your question and has an easy to use code example.
.NET Object Collections Using Generics 101:
Apr 28, 2010 02:42 AM|shyam_oec|LINK
oh dude,i dont want to use generic class for my such simple application. though i want to test just simple thingh,i want to use only generic method
and not generic class.
Can u help me how to use only generic methods
that i have given in my previous post
Apr 28, 2010 02:54 PM|atconway|LINK
I have not worked much with 'Generic Procedures' (as they are referred to in VB.NET), but this MSDN link appeals to me and should be able to help you with nice descriptions and easy to follow code examples:
Generic Procedures in Visual Basic:
Here is another example I found for printing array values using a Generic Procedure in VB.NET:
Using generic methods to print arrays of different types:
Apr 29, 2010 06:23 AM|shyam_oec|LINK
My real question is how to add(sum) two numbers through generic method. i don't want just showing the values.Can any one help me?
Apr 29, 2010 09:43 AM|atconway|LINK
how to add(sum) two numbers through generic method
Well this can be done by using a Lambda expression as well; this saves from always manually adding the values or for having to create a fully qualified method that might only be needed for a few repeated calls:
'Create a Lambda Expression that takes (2) values and adds them together
Dim AddFunc = Function(num1, num2) num1 + num2
Dim MyTotalVal1 As Integer = AddFunc(2, 5)
'?MyTotalVal = 7
Dim MyTotalVal2 As Integer = AddFunc(3, 1)
'?MyTotalVal = 4
Apr 30, 2010 04:36 AM|shyam_oec|LINK
Thanks for reply.Are lambda expressions, alternative to Generic Methods ? Is this same feature possible through Generic Methods.
Look, the basic problem with Generic Methods for finding sum of two numbers is that, Generic Methods do'nt know the meaning of '+' operator. If anyhow we can define the meaning of '+' operator then probably sum is possible through Generic methods.Can
we do operator overloading here? But i do not want to use Classes.
Or, sum is certainly not possible through Generic Methods?
Apr 30, 2010 03:00 PM|atconway|LINK
Are lambda expressions, alternative to Generic Methods ?
Yes I would say it is another way to get something done. In your case you needed to add those (2) numbers together and the Lambda expression will do just that. I think the Lambda expression is much more streamlined and easy to use as well. I am not sure
why you have to use Generic Methods, or if you are just challenging yourself to learn something new. In that case I would focus more on Lambda Expressions, because as you being to get into LINQ you will need to understand them anyways.
I have no further advice on making this work with Generic Procedures as I have not used them, so maybe someone else can respond. However the code I provided does work, and can be expanded on if needed.
May 01, 2010 04:41 AM|shyam_oec|LINK
May 20, 2010 07:52 AM|shyam_oec|LINK
As days are passing and solution for this is not coming out, you know this question has became a nightmare for me.
What should i code to implement the meaning of "+" in generic methods...still trying
May 20, 2010 08:40 AM|atconway|LINK
Just as advice; since asking about the '+' operator is essentially a new question that doesn't bear on the previous posts, please post a new thread on this or another appropriate forum. You will get much more visibility on a new thread/question.
May 27, 2010 03:15 AM|shyam_oec|LINK
solved my problem. There is no need for thinking about overloading "+" operator in Generic Methods ,i solved my problem.You can see this code, and i want to ask you, is there any loophole in this code? since i only wanted to add two variables of any type ?
Public Sub myGeneric2(Of Gen)(ByVal x As Gen, ByVal x1 As Gen)
Dim y As Gen = x
Dim y1 As Gen = x1
Dim a As Object = TryCast(y, Object)
Dim a1 As Object = TryCast(y1, Object)
Dim a2 As Object = a + a1
May 27, 2010 09:06 AM|atconway|LINK
Dim a2 As Object = a + a1
I think the main 'loophole' you may run into is with the line above. Depending on the values that are cast to objects, you may receive something along the following lines:
"Operator '+' is not defined for type 'a' and type 'b'" where 'a' and 'b' might be class objects. However if the types provided to the method arguments are simply integers (say x = 1, x1 = 2) then the Response.Write will produce '3' without error. You
may want to add some exception handling to the TryCast statements to ensure that the types can be added together.