473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best practice for enumerating string constants

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
Nov 20 '05 #1
4 2577
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:%2****************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2
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,

Nov 20 '05 #3
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>
Nov 20 '05 #4
Thanks guys

"Shayne H" <shaynehATlycosSPAMGOTOHELLcoDOTuk> wrote in message
news:#V**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shayne H | last post by:
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
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: Ronald S. Cook | last post by:
It seems to make more sense to separate app settings (e.g. global variables, constants that the development team would control) from web settings (e.g. database connection strings) that the...
5
by: Schoo | last post by:
I have some program constants that I need to set and I thought it would be best to set them all up in a central location so that I can change them quickly when we go from development to production....
5
by: Amelyan | last post by:
I am struggling here trying to determine what is a good programming practice as far as referencing your URLs. When you use Response.Redirect, do you use 1) Hard-coded string --...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
2
by: Ethan V | last post by:
I have about 50 string constants that I put all of them in one file. I find that it's too un-organized because I have to intellisense thru lots of unrelated constant to get to the one I want. Is...
9
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a)...
4
by: Smithers | last post by:
I understand there is no "right" or "wrong" answer to the question posited below. But I'd appreciate some feedback on the "best practice" considerations - if there are any. So here goes: Are...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.