Hello all and happy new year.
I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported.
I've come up with a way around it that lets you create a class which behaves exactly like the regular enumerators (it offers all the functionality a regular enumerator has), and you can store any type of string in it.
The full article can be found here:
http://cookingwithcsha rp.blogspot.com/2007/12/999-c-string-enumerator.html
I hope you find this handy.
All the best!
1 1492 Plater 7,872
Recognized Expert Expert
A nice idea, but your abstract class doesn't really offer any functionality.
The meat is all still done manually in your concrete class: -
private static List mPossibleEnumValues = new List()
-
{ "Toyota", "Honda", "Ford", "Chrysler", "Volvo", "General Motors" };
-
-
static public ECarManufacturers Toyota
-
{
-
get
-
{
-
return new ECarManufacturers(mPossibleEnumValues[0]);
-
}
-
}
-
static public ECarManufacturers Honda
-
{
-
get
-
{
-
return new ECarManufacturers(mPossibleEnumValues[1]);
-
}
-
}
-
static public ECarManufacturers Ford
-
{
-
get
-
{
-
return new ECarManufacturers(mPossibleEnumValues[2]);
-
}
-
}
-
Seems to me your doing all the work right there manually. Not to mention that using .Ford will only return to you a string that says "Ford".
Seems you could bypass all of that and either use JUST the List object, or use a Dictionary or some other collection.
I'm trying to think of a time where having a string as the value would be usefull?
If you have an enum, you can just it's .ToString() to get it's name.
So if you have: -
public enum SMTPAuthTypes
-
{
-
-
NONE = 0,
-
-
PLAIN = 1,
-
-
LOGIN = 2,
-
-
CRAM_MD5 = 3,//maybe dis-allow this one?
-
}//end of enum
-
Then you can do this: -
string mystring=SMTPAuthTypes.LOGIN.ToString();
-
//now mystring="LOGIN"
-
You might also find this thread where I asked about ENUMs useful: http://www.thescripts.com/forum/thread733730.html Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bob Shafer |
last post by:
Is it possible to create dynamic enumerators in Visual
Basic .Net? For example, an enumurator that provides a
dropdown of available SQL servers?
|
by: Matt Taylor |
last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd
like do is to use template specialization to prevent invalid combinations
from compiling. Thus one could not accidentally add a 16-bit register and an
8-bit register since there is no encoding for this on the x86 architecture.
My trouble has stemmed from the fact that enumerators are only integers and
can be freely cast into other enumerators, and I was using them...
|
by: Dave |
last post by:
I have several enums that are generated by a code generator (and I have
no control over the code generator), the problem is that the names are
pretty long
enum VeryLoooooooongEnumName
{
veryLoooooooooong1,
veryLoooooooooong2,
......
veryLoooooooooong10
|
by: Peter Rilling |
last post by:
A design pattern question.
As you know, an enumerator in .NET is broken into two interface (IEnumerable
and IEnumerator). Is there a benefit in having two interfaces? Why not
just have the IEnumerator interface with the methods that interact with the
collection?
|
by: Andrew Quine |
last post by:
Hi
Short one: looking for a good example and an explanation of such
enumerators. I know MessageEnumerator and ResourceReader do this, for
example, but what is the implementation of the enumerator doing that
needs a Dispose of its own?
Cheers!
Andrew Quine
| |
by: juan |
last post by:
hi i have an arraylist that has doctor objects with various properties,
members and methods.
i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
|
by: Tim Davis |
last post by:
I am currently writing a class which I would like to make "enumerable" by
inheriting from IEnumerable. The documentation says that the
IEnumerator.MoveNext method should throw an exception if the object being
enumerated has changed since the enumerator was created. Does anyone have
any tips on how best to implement this?
I can think of 2 approaches, neither of which seem very satisfactory. These
are as follows:
1. Take a copy of the...
|
by: Sanjay Pais |
last post by:
Is there anyway I can define an enum to have a value of a string or
character type?
for example I would like to have
public enum HOW_GOOD
{
AWESOME = "A",
GREAT= "G",
|
by: nitindel |
last post by:
Hi All,
May i have an in depth article of enumerators in C#..that explains the Enumerators in C# very precisely....
Thanks,
Nitin
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |