Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:21:45+00:00 2026-05-28T16:21:45+00:00

I tend to loath repetition in code, so when I come across a problem

  • 0

I tend to loath repetition in code, so when I come across a problem where the only different is types I tend to use generics. Coming from a C++ background I find vb.net’s version to be rather frustrating, I know C++ has template specialization and I guess vb.net does not
so what I have is a set of routines that do the exact same code regardless of type being passed.

something like this

Public Sub decision(Of T)(ByVal a As T, ByVal b As Integer)
  If b > 10 then
    gt(a)
  Else
    lt(a)
  End If
End Sub

I am only ever passing two types to routine, Strings and Integers and what those routines do with string differs from what it does with integers.

Public Sub gt(ByVal a As String)
Public Sub gt(ByVal a As Integer)

Public Sub lt(ByVal a As String)
Public Sub lt(ByVal a As Integer)

This is where I get frustrated with vb.net vs C++, AFAIK, C++ would check the types at compile time and only against what types are sent to decision. However, in vb.net I get an error that type T cannot be converted to String or Integer

Error   3   Overload resolution failed because no accessible 'gt' can be called with these arguments:
    'Public Sub gt(a As String)': Value of type 'T' cannot be converted to 'String'.
    'Public Sub gt(a As Integer)': Value of type 'T' cannot be converted to 'Integer'.

I tried constraints Public Sub decision(Of T As {String, Integer})(ByVal a As T, ByVal b As Integer) but constraints need to be Inheritable classes so neither String, nor Integer can be used.

My Next Solution was to add a generic versions of gt and lt:

Public Sub lt(Of T)(ByVal a As T)
  Debug.Fail("Not Implemented")
End Sub

Public Sub lt(Of T)(ByVal a As T)
  Debug.Fail("Not Implemented")
End Sub

And hey! no more compile errors however the only routine that gets called is the generic version of gt and lt. Which I guess makes sense in light of the previous cannot convert errors. I’ve encountered this issue before where there are non-generic overloads of a generic routine, I could not find a solution then, and I cannot find a solution now.

Is there something I am missing that would make this type of overloading possible?

Edit: a complete working example

Module Module1
   Sub Main()

      decision(1, 5)
      decision(1, 10)
      decision("hello world", 5)
      decision("hello world", 10)

   End Sub


   Public Sub decision(Of T)(ByVal a As T, ByVal b As Integer)
      If b > 10 Then
         gt(a)
      Else
         lt(a)
      End If
   End Sub

   Public Sub gt(ByVal a As String)
      Debug.WriteLine(" gt string:  " + a)
   End Sub
   Public Sub gt(ByVal a As Integer)
      Debug.WriteLine(" gt integer: " + a.ToString)
   End Sub

   Public Sub lt(ByVal a As String)
      Debug.WriteLine(" lt string: " + a)
   End Sub
   Public Sub lt(ByVal a As Integer)
      Debug.WriteLine(" lt integer: " + a.ToString)
   End Sub

#If False Then
   Public Sub gt(Of T)(ByVal a As T)
      Debug.Fail("Not implemented")
   End Sub
   Public Sub lt(Of T)(ByVal a As T)
      Debug.Fail("Not implemented")
   End Sub
#End If
End Module
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:21:46+00:00Added an answer on May 28, 2026 at 4:21 pm

    From Differences Between C++ Templates and C# Generics (the same holds for VB .NET):

    C++ allows code that might not be valid for all type parameters in the
    template, which is then checked for the specific type used as the type
    parameter. C# requires code in a class to be written in such a way
    that it will work with any type that satisfies the constraints. For
    example, in C++ it is possible to write a function that uses the
    arithmetic operators + and – on objects of the type parameter, which
    will produce an error at the time of instantiation of the template
    with a type that does not support these operators. C# disallows this;
    the only language constructs allowed are those that can be deduced
    from the constraints.

    I’m not able to solve your problem with .NET Generics. But you can avoid repeating the logic by using lambdas and closures, which I also think is a more natural way to do it in .NET:

    Public Sub Decision(ByVal a As String, ByVal b As Integer)
        Decision(b, Sub() gt(a), Sub() lt(a))
    End Sub
    
    Public Sub Decision(ByVal a As Integer, ByVal b As Integer)
        Decision(b, Sub() gt(a), Sub() lt(a))
    End Sub
    
    Private Sub decision(ByVal b As Integer, ByVal gt As Action, ByVal lt As Action)
        If b > 10 Then
            gt()
        Else
            lt()
        End If
    End Sub
    
    Public Sub gt(ByVal a As String)
        Debug.WriteLine(" gt string:  " + a)
    End Sub
    Public Sub gt(ByVal a As Integer)
        Debug.WriteLine(" gt integer: " + a.ToString)
    End Sub
    
    Public Sub lt(ByVal a As String)
        Debug.WriteLine(" lt string: " + a)
    End Sub
    Public Sub lt(ByVal a As Integer)
        Debug.WriteLine(" lt integer: " + a.ToString)
    End Sub 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tend to use certain options more than others at different times during my
I tend to align code on equal signs for better readability. From this: $
I tend to use enums to create mini data tables all over my code.
I tend to use the following pattern a lot of different places for a
Sometimes I tend using autocomplete from eclipse (3.5) for anonymous inner types. For some
I tend to use a StatusStrip at the bottom of most of my applications
I tend to use the bisect command in git extensively. Now I want to
I always tend to run into the following design problem that I'm never quite
In our company we tend to use views and stored procedures. We've recently started
I tend put multiple lines of code that fullfill a certain task into blocks,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.