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

  • SEARCH
  • Home
  • 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 3349268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:38:36+00:00 2026-05-18T01:38:36+00:00

So I implemented my own form of Enum static classes so I can associate

  • 0

So I implemented my own form of Enum static classes so I can associate strings to enumeration values. One of the shared functions in each enum class, GetValue, is used to quickly look up the string value from an internal array via the offset. While all of the enum classes implement the same exact function, the data types of their parameters differ, specific to the enums in each class.

So I come to a point where I want to create a generic class that can use any of the enums by passing it as a generic type parameter. But I cannot find a way to constrain type T in such a way to be able to call each enum’s GetValue function. This is where an interface would come in handy, but VB.NET forbids interfaces for shared methods.

I have a base class that I could constrain to, but the base class doesn’t implement the GetValue method, and I can’t define a generic version of it, because I need to rely on shared properties of each child class in order for GetValue to do its thing.

Is there another way to tackle this?

EDIT:
Here’s what an example enum class looks like. The parent, EBase is nothing special, just hosting common Name, Value, and Type properties, plus two shared operators, op_Equality and op_Inequality. Imagine using a couple of these enum classes with a generic method (or class) and needing to access the GetValue member via the generic type parameter T.

Friend NotInheritable Class EExample
    Inherits EBase

    Private Sub New()
    End Sub


    Friend Shared Function GetValue(ByVal Name As String) As Enums
        Return Enums(Array.IndexOf(_Names, Name))
    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), 0)
    Friend Shared ReadOnly TwoB As New Enums(_Names(1), 1)
    Friend Shared ReadOnly ThreeC As New Enums(_Names(2), 2)
    Friend Shared ReadOnly FourD As New Enums(_Names(3), 3)
    Friend Shared ReadOnly FiveE As New Enums(_Names(4), 4)


    '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
            MyBase.Type = GetType(Enums)
        End Sub
    End Class
End Class

EDIT2:
Here’s how the things are used:

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

will print two_boy

  • 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-18T01:38:37+00:00Added an answer on May 18, 2026 at 1:38 am

    How about this as a solution:

    Friend NotInheritable Class EExample
        Inherits EBase
    
        Protected Sub New()
        End Sub
    
        Protected Overrides Function BuildValues() As EBase.Enums()
            Return New Enums() _
            { _
                Build(Of EExample)("one_adam", 0), _
                Build(Of EExample)("two_boy", 1), _
                Build(Of EExample)("three_charles", 2), _
                Build(Of EExample)("four_david", 3), _
                Build(Of EExample)("five_edward", 4) _
            }
        End Function
    
        Private Shared _instance As EExample = New EExample()
    
        Friend Shared ReadOnly OneA As Enums = _instance.Values(0)
        Friend Shared ReadOnly TwoB As Enums = _instance.Values(1)
        Friend Shared ReadOnly ThreeC As Enums = _instance.Values(2)
        Friend Shared ReadOnly FourD As Enums = _instance.Values(3)
        Friend Shared ReadOnly FiveE As Enums = _instance.Values(4)
    
    End Class
    

    EBase then looks like this:

    Public MustInherit Class EBase
    
        Protected MustOverride Function BuildValues() As Enums()
    
        Private _values As EBase.Enums()
    
        Friend ReadOnly Property Values() As EBase.Enums()
            Get
                If _values Is Nothing Then
                    _values = Me.BuildValues()
                End If
                Return _values
            End Get
        End Property
    
        Friend ReadOnly MaxAlterEnums As Int32 = Me.Values.Length
    
        Friend Function GetValue(ByVal Name As String) As Enums
            Return Me.Values.Where(Function(n) n.Name = Name).FirstOrDefault()
        End Function
    
        Friend Shared Function Build(Of T As EBase)(ByVal name As String, ByVal value As Int32) As Enums
            Return New Enums(name, value, GetType(T))
        End Function
    
        Public Class Enums
            Public Sub New(ByVal name As String, ByVal value As Int32, ByVal type As Type)
                Me.Name = name
                Me.Value = value
                Me.Type = type
            End Sub
            Private _name As String
            Public Property Name() As String
                Get
                    Return _name
                End Get
                Set(ByVal Value As String)
                    _name = Value
                End Set
            End Property
            Private _value As Integer
            Public Property Value() As Integer
                Get
                    Return _value
                End Get
                Set(ByVal Value As Integer)
                    _value = Value
                End Set
            End Property
            Private _type As Type
            Public Property Type() As Type
                Get
                    Return _type
                End Get
                Set(ByVal Value As Type)
                    _type = Value
                End Set
            End Property
        End Class
    
    End Class
    

    Now your sample code works exactly as before:

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

    And you are dealing with plain instances for you code implementation. The only Shared values are the five Enums and a private _instance variable.

    You can now restrict on EBase.

    Does this work for you?


    Kumba pointed out that I didn’t expose the GetValue function on the EExample class.

    Just add this to EExample:

        Friend Shared Function GetValue(ByVal Name As String) As Enums
            Return _instance.GetValue(Name)
        End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've implemented my own event handler and added it to the selection model of
Is it possible to implement autoboxing for your own classes? To illustrate my example,
I want to implement my own clustering algorithm using this Virtual Earth javascript API:
Yesterday I thought it would be nice to implement my own Trigger in a
At my current job, we're looking to implement our own odbc driver to allow
Implemented the Sink Class - to receive event notifications from COM Server Event Interface
I implemented a Lucene search solution awhile back, and it got me interested in
I implemented a small OOP library in Lua, and two things are not quite
Already implemented performance boosters : - Get compatible image of GraphicsConfiguration to draw on
I implemented OpenID support for an ASP.Net 2.0 web application and everything seems 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.