Best practice for enumerating string constants

  • Thread starter Thread starter Shayne H
  • Start date Start date
S

Shayne H

What is the best way to enumerate a grouping of strings?
The way I have been doing it is:

Public Enum PlatformID
Unknown
Win16
Win32
Win32NT
WinCE
End Enum

Private Shared sPlatform() As String = { _
"Unknown", _
"Microsoft Windows 16-bit", _
"Microsoft Windows 32-bit", _
"Microsoft Windows NT 32-bit", _
"Microsoft Windows CE"}

Private Shared Function PlatformToString(ByVal platform As
OSInfo.PlatformID) As String
Return sPlatform(platform)
End Function

Is there a better way to do this?
I feel that the strings should be constants rather than stored in a private
array field, but I cannot see a way other than a really long select case
statement.
 
Shayne,
I would consider adding a custom attribute to each value of the Enum, then
in the PlatformToString use Reflection to retrieve the value of the custom
attribute.

This way the meta data about the value (the description) is associated
directly with the value. Of course if you did not author the enum this won't
work ;-)

Another option I would consider is to store the values in a Resource file
(.resx) then using System.Resources.ResourceManager retrieve the description
based on the text name of the enum value. The benefit of this method is its
easy to internationalize.

I do not have specific examples of either of the above, post if you have
questions implementing either.

Hope this helps
Jay
 
What is the best way to enumerate a grouping of strings?
The way I have been doing it is:

Public Enum PlatformID
Unknown
Win16
Win32
Win32NT
WinCE
End Enum

I'm guessing you're not talking about System.PlatformID.
Private Shared sPlatform() As String = { _
"Unknown", _
"Microsoft Windows 16-bit", _
"Microsoft Windows 32-bit", _
"Microsoft Windows NT 32-bit", _
"Microsoft Windows CE"}

I don't suppose this is what you're after...

Dim oPlatformID As PlatformID
oPlatformID = PlatformID.Win32
MsgBox( oPlatformID.ToString() )

If internationalisation is an issue, then you should use resources.
(Unlikely, as "Microsoft Windows CE" is the same in most languages)

If you require spaces (etc.) to be in the name, the best way is to
implement a singleton class that wraps up a StringDictionary
(initialised in its constructor) - see below.

And if you fancy being a pedant, you should implement a Platform class
instead of an enum, a PlatformCollection of some sorts (an inherited
HybridDictionary is a good one), a PlatformFactory object to serialise
the objects from resources (and implement the GetCurrentPlatform
method)... and be sure to implement IComparable and IEnumerable on the
Platform and PlatformCollection respectively. Infact, inherit your own
IPlatformEnumerable so that you can iterate though any other ADT
collections you decide to implement. Ooh! And don't forget to
implement an IPlatform interface in case anyone wants to alter the
implementation of a Platform... ;]

More seriously, the following allows for future extension (and the
model can be applied to other things... for your reference)

Public Class PlatformFactory
Private Shared mPlatformMap As HybridDictionary

Shared Sub New()
mPlatformMap = New HybridDictionary()
mPlatformMap.Add(PlatformID.Unknown, "Unknown")
mPlatformMap.Add(PlatformID.Win16, "Microsoft Windows 16-bit")
' etc...
End Sub

Public Shared Function GetName(ByVal oPlatformID As PlatformID) _
As String
Return mPlatformMap(oPlatformID)
End Function

End Class


Rgds,
 
Hi Shayne,

I can't say I've developed any respect for Enums yet - too many
limitations against too few advantages.

As always, VB./NET has several ways to skin a cat.
Here's a way of doing the job with a Class and a Structure.

A bit of a palaver, perhaps - but it works! ;-)

Regards,
Fergus

<code>
Public Class Platform
Public Structure IdName
Private _Id As Integer
Private _Name As String
Public Sub New (Id As Integer, Name As String)
_Id = Id : _Name = Name
End Sub
Public ReadOnly Property Id As Integer
Get
Return _Id
End Get
End Property
Public Overrides Function ToString As String
Return _Name
End Function
End Structure

Public ReadOnly Shared Unknown As New IdName (0, "Unknown")
Public ReadOnly Shared Win16 As New IdName (1, "Win 16-bit")
Public ReadOnly Shared Win32 As New IdName (2, "Win 32-bit")
Public ReadOnly Shared Win32NT As New IdName (3, "Win NT 32-bit")
Public ReadOnly Shared WinCE As New IdName (4, "Win CE")
End Class

S = Platform.Unknown.Id & ", " & Platform.Unknown.ToString
</code>
 
Back
Top