473,624 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 PlatformToStrin g(ByVal platform As
OSInfo.Platform ID) As String
Return sPlatform(platf orm)
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 2598
Shayne,
I would consider adding a custom attribute to each value of the Enum, then
in the PlatformToStrin g 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.Resource s.ResourceManag er retrieve the description
based on the text name of the enum value. The benefit of this method is its
easy to internationaliz e.

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

Hope this helps
Jay

"Shayne H" <shaynehATlycos SPAMGOTOHELLcoD OTuk> wrote in message
news:%2******** ********@tk2msf tngp13.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 PlatformToStrin g(ByVal platform As
OSInfo.Platform ID) As String
Return sPlatform(platf orm)
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"
<shaynehATlycos SPAMGOTOHELLcoD OTuk> 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.Platform ID.

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.Win3 2
MsgBox( oPlatformID.ToS tring() )

If internationalis ation 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 StringDictionar y
(initialised in its constructor) - see below.

And if you fancy being a pedant, you should implement a Platform class
instead of an enum, a PlatformCollect ion of some sorts (an inherited
HybridDictionar y is a good one), a PlatformFactory object to serialise
the objects from resources (and implement the GetCurrentPlatf orm
method)... and be sure to implement IComparable and IEnumerable on the
Platform and PlatformCollect ion respectively. Infact, inherit your own
IPlatformEnumer able 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 HybridDictionar y

Shared Sub New()
mPlatformMap = New HybridDictionar y()
mPlatformMap.Ad d(PlatformID.Un known, "Unknown")
mPlatformMap.Ad d(PlatformID.Wi n16, "Microsoft Windows 16-bit")
' etc...
End Sub

Public Shared Function GetName(ByVal oPlatformID As PlatformID) _
As String
Return mPlatformMap(oP latformID)
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.Unknow n.Id & ", " & Platform.Unknow n.ToString
</code>
Nov 20 '05 #4
Thanks guys

"Shayne H" <shaynehATlycos SPAMGOTOHELLcoD OTuk> wrote in message
news:#V******** ******@tk2msftn gp13.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 PlatformToStrin g(ByVal platform As
OSInfo.Platform ID) As String
Return sPlatform(platf orm)
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
421
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
9296
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
0
1786
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 operations team would control... or does it? Our ops team doesn't like that they must comment out connection string lines depending on wjhere deploying and would rather have their own file that they insert as desired. But we currently require...
5
4191
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. What is the best way to do that? Should I put them in web.config? Should I create a special class for these settings? Wherever it is best to write them, can you give me a line or 2 of code to show the best way to set a constant string? ...
5
1797
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 -- Response.Redirect("MyPage.aspx"); 2) Constants -- Response.Redirect(STRMyPage); // where input parameter is -> const string STRMyPage = "MyPage.aspx"; 3) Something entirely differnent?
17
8024
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 read or modify this string so I don't want to store it in an INI / Text file or in registery. Can someone please tell me the best practice for this. Thanks Mike
2
1311
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 there a best practice for organizing constant? Please share with me your thoughts and experiences. Thanks so much in advance.
9
2534
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) read-only mode or b) write-only mode, - a function picking points a) above, b) below or c) contained on a plane in 3D, etc.
4
1572
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 public readonly properties generally frowned upon? If so, then are there any generally accepted cases where they are considered as okay? Please consider the following class and notice it has two properties - EventMessage1 and EventMessage2.
5
13481
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 will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.