user3278315
user3278315

Reputation: 15

Performance Enum vs class?

i found few new style (for me) to "define" output from select query.

Private Enum Item
    ID
    Item
    Description
End Enum


    Private Class Item
    Private ID as String
    Private Item as String
    Private Desc as String
    End Class

I 'm thinking of using either one of them. by using class i does not need to re-cast the element type before i display. but Enum seems like easier to understand.

Anyone have some suggestion how to decide?

Upvotes: 1

Views: 313

Answers (2)

Enum members are numeric (usually integer, but can be long). But they are not variable and do not change at runtime. So your enum equates to:

Private Enum Item
    ID = 0
    Item = 1
    Description = 2
End Enum

If you want Description to be a string, then a class is a better idea. Enums are used to reference or index something or limit/define a selection. Like:

Public Property Stooge As Stooges

Friend Enum Stooges
    Larry
    Moe
    Curly
    Shemp
    CurlyJoe
End Enum

The Stooge Property must be one of those values. in code it will show you the text ("moe") but store and integer (1). users will be shown the text in drop downs etc.


You can associate a description with Enum constants:

Public Enum Stooges
    <Description("Larry - Funny one")> Larry
    <Description("Moe - 'Smart' One")> Moe
    <Description("Curly - Sore One")> Curly
    <Description("Shemp - One with bad haircut")> Shemp
    <Description("CurlyJoe - Last one")> CurlyJoe
End Enum

To get the description for a single one:

Public Shared Function GetDescription(ByVal EnumConstant As [Enum]) As String
    Dim fi As Reflection.FieldInfo = 
              EnumConstant.GetType().GetField(EnumConstant.ToString())

    Dim attr() As DescriptionAttribute =
           DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), 
           False), DescriptionAttribute())

    If attr.Length > 0 Then
        Return attr(0).Description
    Else
        Return EnumConstant.ToString()   ' return enum name if no Descr
    End If
End Function

Usage: str = enumHelper.GetDescription(Stooge.Moe) (enumHelper is the name of the calss where the static/shared function resides).

To get a String Array of all the descriptions:

Public Shared Function GetDescriptions(ByVal type As Type) As String()
    Dim n As Integer = 0
    Dim enumValues As Array

    Try
        enumValues = [Enum].GetValues(type)
        Dim Descr(enumValues.Length - 1) As String

        For Each value As [Enum] In enumValues
            Descr(n) = GetDescription(value)
            n += 1
        Next
        Return Descr

    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return Nothing
    End Try

End Function

Usage: Dim strEnum As String() = enumHelper.GetDescriptions(GetType(Stooges))

Upvotes: 2

jmoreno
jmoreno

Reputation: 13561

From your question, what you really mean is Struct vs Class. I would default to creating a class. The main reason to use a struct vs a class, is when you need value semantics -- assignment/parameters copies the bits, not a pointer. This is fairly rare in my experience. Unless you have a compelling reason (and you know the difference), go with a class.

Upvotes: 1

Related Questions