473,851 Members | 2,287 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 1503
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
1806
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
1818
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
1868
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
1373
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
1705
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
3454
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
2026
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
2795
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
1308
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
9744
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
11011
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...
1
10725
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
10352
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
9503
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
7072
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
5931
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4547
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
3
3178
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.