locked
how to use generic methods in VB.NET RRS feed

  • Question

  • User-1976290245 posted

    Hi everyone,

    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.

    Thanks. 

    Saturday, April 24, 2010 1:19 AM

Answers

  • User-1491938696 posted

    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

       End Sub


    End Class


    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)

    Response.Write(gen.V1 + gen.V2)

    ' Output: 3

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 24, 2010 6:02 AM
  • User-952121411 posted

    Actually I covered this is detail in the link below! Smile  It should help you out in understanding your question and has an easy to use code example.

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 27, 2010 4:31 PM
  • User-952121411 posted

    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


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2010 9:43 AM
  • User-952121411 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 30, 2010 3:00 PM
  • User-952121411 posted

    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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 27, 2010 9:06 AM

All replies

  • User-1491938696 posted

    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

       End Sub


    End Class


    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)

    Response.Write(gen.V1 + gen.V2)

    ' Output: 3

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 24, 2010 6:02 AM
  • User-1976290245 posted

    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
        End Sub


     

    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?

    Sunday, April 25, 2010 11:30 PM
  • User-1976290245 posted

    HI,

    What happend? Is there no any answer to my question ? Or my question is absurd? 

    Tuesday, April 27, 2010 3:02 AM
  • User-952121411 posted

    Actually I covered this is detail in the link below! Smile  It should help you out in understanding your question and has an easy to use code example.

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 27, 2010 4:31 PM
  • User-1976290245 posted

    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

    Wednesday, April 28, 2010 2:42 AM
  • User-952121411 posted

    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: 

    http://msdn.microsoft.com/en-US/library/ms235246(v=VS.80).aspx

    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:

    http://www.java2s.com/Tutorial/VB/0180__Generics/Usinggenericmethodstoprintarraysofdifferenttypes.htm

     

    Wednesday, April 28, 2010 2:54 PM
  • User-1976290245 posted

    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? 

    Thursday, April 29, 2010 6:23 AM
  • User-952121411 posted

    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


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2010 9:43 AM
  • User-1976290245 posted

    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?

    Anyway thanks.

    Friday, April 30, 2010 4:36 AM
  • User-952121411 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 30, 2010 3:00 PM
  • User-1976290245 posted
    Yes, Lambda expression are just another way of getting something done.And you are probably right that i have just challenged myself so solve this problem through Generic Method.I am on the way to solve how to define the meaning of '+' operator for Genetic Methods without using another Class. Lambda Expressions are really simple and beautiful,yet powerful. Thanks.
    Saturday, May 1, 2010 4:41 AM
  • User-1976290245 posted

    Hi,

    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

    Thursday, May 20, 2010 7:52 AM
  • User-952121411 posted

    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. Smile

    Thursday, May 20, 2010 8:40 AM
  • User-1976290245 posted

    Hi,

    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
    
            Response.Write(a2)
     End Sub


     

    Thursday, May 27, 2010 3:15 AM
  • User-952121411 posted

    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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 27, 2010 9:06 AM