473,388 Members | 1,600 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,388 software developers and data experts.

How implement a string based enum

Hi,

I need to have something like (wich as written of course is not possible)

public enum CountryType : String
{
USA = "US",
UnitedKingdom = "UK",
Germany = "DE",
Denmark = "DK"
}

Well, it has to be a sealed class of some sort with a lot of static fields -
but how do i get same functionallity as with a regular enum

Kr.
Soren
Nov 17 '05 #1
2 3037
Soeren S. Joergensen wrote:
I need to have something like (wich as written of course is not possible)

public enum CountryType : String
{
USA = "US",
UnitedKingdom = "UK",
Germany = "DE",
Denmark = "DK"
}

Well, it has to be a sealed class of some sort with a lot of static fields
- but how do i get same functionallity as with a regular enum


I guess that depends on what functionality exactly you mean - it won't be
possible to reproduce the enum functionality in every detail, because your
class won't really be an enum, whatever you do.

But you could do something like this to start with:

public static class Country {
private Country(string stringValue) {
this.stringValue = stringValue;
}
private string stringValue;

public static readonly Country USA = new Country("US");
public static readonly Country UnitedKingdom = new Country("UK");
public static readonly Country Germany = new Country("DE");
public static readonly Country Denmark = new Country("DK");
}
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #2
i achive something similar by creating static fields in a class like this:

public sealed class CountryType
{
public static string USA { get { return "US"; } }
public static string UnitedKingdom { get { return "UK"; } }
public static string Germany { get { return "DE"; } }
}
Soeren S. Joergensen wrote:
Hi,

I need to have something like (wich as written of course is not possible)

public enum CountryType : String
{
USA = "US",
UnitedKingdom = "UK",
Germany = "DE",
Denmark = "DK"
}

Well, it has to be a sealed class of some sort with a lot of static fields -
but how do i get same functionallity as with a regular enum

Kr.
Soren

Nov 17 '05 #3

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

Similar topics

11
by: Kai-Uwe Bux | last post by:
Hi folks, does anyone see a way to implement the following enum_limits template: template < typename EnumType > struct enum_limits { // first element in range of EnumType: static...
5
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 =...
2
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
4
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
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
5
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was...
9
by: Mike Hofer | last post by:
BACKGROUND: We've designed a Website for a client that will be deployed across multiple physical locations. The site will be hosted from a corporate NOC, and administered by the IT group there. ...
0
by: Chad Dokmanovich | last post by:
If you could answer this, I would be very appreciative: Why does this code die on line 63 with the error "Specified method is not supported." error message? 1 Imports...
8
by: John | last post by:
Hi, gurus, How can I implement the following feature in C#: Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ", group") For Each objMember In objGroup.Members...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.