PC Review


Reply
Thread Tools Rate Thread

Best practice for enumerating string constants

 
 
Shayne H
Guest
Posts: n/a
 
      12th Oct 2003
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.

--
Thanks for any help,
Shayne H


 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      12th Oct 2003
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

"Shayne H" <shaynehATlycosSPAMGOTOHELLcoDOTuk> wrote in message
news:%(E-Mail Removed)...
> 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.
>
> --
> Thanks for any help,
> Shayne H
>
>



 
Reply With Quote
 
_Andy_
Guest
Posts: n/a
 
      12th Oct 2003
On Mon, 13 Oct 2003 00:25:17 +0930, "Shayne H"
<shaynehATlycosSPAMGOTOHELLcoDOTuk> wrote:

>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,

 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      13th Oct 2003
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>


 
Reply With Quote
 
Shayne H
Guest
Posts: n/a
 
      14th Oct 2003
Thanks guys

"Shayne H" <shaynehATlycosSPAMGOTOHELLcoDOTuk> wrote in message
news:#(E-Mail Removed)...
> 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.
>
> --
> Thanks for any help,
> Shayne H
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enumerating public constants from class bz Microsoft C# .NET 2 25th Aug 2009 09:40 PM
string constants / conversion from const char * to String kelvin.koogan@googlemail.com Microsoft VC .NET 5 25th Jun 2007 01:35 PM
Reflection: enumerating constants Jeff Johnson Microsoft Dot NET Framework 1 10th Nov 2006 05:46 AM
integer constants used with string constants Eric Newton Microsoft Dot NET Framework 3 14th Jan 2004 05:05 AM
Best practice for enumerating string constants Shayne H Microsoft Dot NET 1 13th Oct 2003 03:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:40 AM.