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 3595456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:51:16+00:00 2026-05-18T19:51:16+00:00

I am assuming No, but I cannot find conclusive proof on Google to back

  • 0

I am assuming “No”, but I cannot find conclusive proof on Google to back this assumption. Using keywords of ‘vb.net “generic operator overload”‘ yields exactly 1 result, and removing ‘overload’ gives more, but no direct statement to the issue.

My thinking is given an abstract class, it’d be great to be able to implement a generic operator overload that a derived class can use in such a case when said operator overload has to return a New copy of the derived class, yet the code for each overload is the same. If that makes any sense.

This touches back to my previous questions on my custom Enum class and overloading the bitwise operators (And, Or, Not, & Xor), but, this particular thought was prompted by a mere curiosity of “Can it be done?”.

Here’s what one of my custom enums basically look like:
The parent, EBase is nothing special, just hosting common Name and Value properties, plus two shared operators, op_Equality and op_Inequality.

Friend NotInheritable Class EExample
    Inherits EBase

    Private Sub New()
    End Sub

    Friend Shared Function GetValue(ByVal Name As String) As Enums
        Dim tmpOffset As Int32 = Array.IndexOf(_Names, Name)
        Return If(HasContent(Name), If(tmpOffset <> -1, Values(tmpOffset), Nothing), Nothing)
    End Function


    ' Num of Enums defined.
    Friend Shared ReadOnly MaxEnums As Int32 = 5

    ' String literals.
    Private Shared ReadOnly _Names As String() = New String() _
        {"one_adam", "two_boy", "three_charles", "four_david", "five_edward"}

    ' Enums.
    Friend Shared ReadOnly OneA As New Enums(_Names(0), 1)
    Friend Shared ReadOnly TwoB As New Enums(_Names(1), 2)
    Friend Shared ReadOnly ThreeC As New Enums(_Names(2), 4)
    Friend Shared ReadOnly FourD As New Enums(_Names(3), 8)
    Friend Shared ReadOnly FiveE As New Enums(_Names(4), 16)


    ' Enum Values Array.
    Friend Shared ReadOnly Values As Enums() = New Enums() _
        {OneA, TwoB, ThreeC, FourD, FiveE}


    Friend NotInheritable Class Enums
        Inherits EBase

        Private Sub New()
        End Sub

        Friend Sub New(ByVal Name As String, ByVal Value As Int32)
            MyBase.Name = Name
            MyBase.Value = Value
        End Sub
    End Class
End Class

Here’s how the things are used:

Dim Foo As EExample.Enums
Foo = EExample.TwoB
Debug.Print(Foo.Name)

will print two_boy

Now, given that, if I want to do the following:

Dim Foo as EExample.Enums
Foo = EExample.OneA Or EExample.FiveE

I have to define an operator overload for Or inside the EExample.Enums definition. How would this operator overload look?

Public Shared Operator Or(ByVal lhOp As Enums, ByVal rhOp As Enums) As Enums
    Return New Enums(String.Concat(lhOp.Name, "|"c, rhOp.Name),
                     lhOp.Value Or rhOp.Value, True)
End Operator

I have to return a new EEXample.Enums object containing the Bitwise-Or’ed Value property of the parent EExample enums. For the name, I just concatenate the Name properties together with a pipe character until I think of something better.

Assume I have 20 enum classes similar to EExample. I have to duplicate all that operator overload code for each definition even though in the IDE, it looks the exact same. In IL, however, each overload is specific to the containing parent enum class:

.method public specialname static class MyAssembly.EExample/Enums 
        op_BitwiseOr(class MyAssembly.EExample/Enums lhOp,
                     class MyAssembly.EExample/Enums rhOp) cil managed
{ ... }

But! A generic operator overload would solve this problem if defined in EBase!

Friend Interface IEnums
    Property Name As String
    Property Value As Int32
End Interface

Public Shared Operator Or(Of T As IEnums)(ByVal lhOp As T, ByVal rhOp As T) As T
    Return New T(String.Concat(lhOp.Name, "|"c, rhOp.Name),
                 lhOp.Value Or rhOp.Value, True)
End Operator

Then (in theory anyways), calling EExample.OneA Or EExample.FiveE would work because the compiler would know to call the generic operator overload from EBase, know that EExample.Enums matches the IEnums interface constraint, and automatically supply T.

That or I’m just swimming up a certain creek here without a paddle and over-analyzing things. But it’s an interesting thought, no? What is StackOverflow’s consensus? Do I need to lay off the Spice a little bit?

PS: I know that, in the last example, Return New T( ... ) is invalid, but I can’t think of a proper syntax that would articulate the basic idea.

  • 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-18T19:51:16+00:00Added an answer on May 18, 2026 at 7:51 pm

    According to what I can see in the language specification, generic operators are not allowed. Section 9.8 says

    The type of at least one of the operands or the return value must be the type that contains the operator.

    and later when it describes the declaration syntax makes no accounting for a generic specifier as methods do in section 9.2.1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sure this has been asked before, but I cannot find it. Basically,
I have an Azman store in AD, but cannot access it from our ASP.NET
I use the ternary operator quite often but I've not been able to find
I wish to run a powerbuilder application but I cannot find the runtime environment
I know it makes little difference to a project but, assuming you use #defined
Assuming I have only the class name of a generic as a string in
I cannot add log4Net as a reference to my new .Net project. I've researched
Just to be clear, I cannot use CLR UDF for this and SUBSTRING and
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming you can't use LINQ for whatever reason, is it a better practice to

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.