473,671 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# string enumerators

muaddubby
8 New Member
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!
Jan 1 '08 #1
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:
Expand|Select|Wrap|Line Numbers
  1. private static List mPossibleEnumValues = new List()    
  2. {        "Toyota",        "Honda",        "Ford",        "Chrysler",        "Volvo",        "General Motors"    };    
  3.  
  4. static public ECarManufacturers Toyota    
  5. {        
  6. get 
  7. return new ECarManufacturers(mPossibleEnumValues[0]); 
  8. }    
  9. }    
  10. static public ECarManufacturers Honda    
  11. {        
  12. get 
  13. return new ECarManufacturers(mPossibleEnumValues[1]); 
  14. }    
  15. }
  16. static public ECarManufacturers Ford    
  17. {        
  18. get 
  19. return new ECarManufacturers(mPossibleEnumValues[2]); 
  20. }    
  21. }
  22.  
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:
Expand|Select|Wrap|Line Numbers
  1. public enum SMTPAuthTypes
  2.     {
  3.  
  4.         NONE = 0,
  5.  
  6.         PLAIN = 1,
  7.  
  8.         LOGIN = 2,
  9.  
  10.         CRAM_MD5 = 3,//maybe dis-allow this one?
  11.     }//end of enum
  12.  
Then you can do this:
Expand|Select|Wrap|Line Numbers
  1. string mystring=SMTPAuthTypes.LOGIN.ToString();
  2. //now mystring="LOGIN"
  3.  

You might also find this thread where I asked about ENUMs useful:
http://www.thescripts.com/forum/thread733730.html
Jan 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1795
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?
6
1808
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...
1
1861
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
2
1365
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?
2
1694
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
5
3448
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++)
6
2021
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...
6
2788
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",
1
1300
nitindel
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
0
8400
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,...
0
8924
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, 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...
0
8823
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 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...
1
8602
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
8672
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...
1
6234
isladogs
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...
0
5702
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();...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.