|
-
Jul 26th, 2006, 07:00 PM
#1
Thread Starter
Lively Member
What is the importance of Enumerations?
Is it used alot in the professional world?
-
Jul 26th, 2006, 07:06 PM
#2
Re: What is the importance of Enumerations?
Absolutely. You use enumerations all the time. Probably one of the most often used is DialogResult. The idea of an enumertaed type is that it assigns user-friendly labels to numerical values. Often the actual values don't really matter, just that they are distinct and constant. The advantage of using an enumerated type over an Integer is that it is easier to remember what each value actually means because the label tells you. The advantage of using an enumerated type over either an Integer or a String is that you limit the possible values to only a small, specific set. if you used an Integer or a string you could use any value at all of that type.
-
Jul 26th, 2006, 07:41 PM
#3
Thread Starter
Lively Member
Re: What is the importance of Enumerations?
If it only represents something then why is it stored in numeric form?
you said the actual value doesn't matter
-
Jul 26th, 2006, 07:51 PM
#4
Re: What is the importance of Enumerations?
Let me go back to the example of DialogResult. What form would you suggest the values get stored in? 32-bit integers are the base currency of 32-bit systems, so it is simplest to store something as a 32-bit integer. It takes more effort and more space to store the string "OK" than it does to the integer 1. The lable 'OK' is to make your life easier when writing high-level code. 'OK' is easier to remember than what 1 means, plus 1 could have numerous other meanings in numerous other circumstances, while System.Windows.Forms.DialogResult.OK has only one specific meaning. Also, by using the type DialogResult you are limiting the code to use only values that are valid DialogResult objects. When you compile your project the low-level code it produces will just use the numerical value because they are nice and easy to deal with and pass around, plus there is no need to make life easier for a human being. It doesn't really matter whether DialogResult is represented by 1, 100, 1000000, -1 or any other number, as long as the value is constant and it is distinct from all other DialogResult values, allowing it to be uniquely identified wherever it is used in code.
-
Jul 26th, 2006, 08:06 PM
#5
Thread Starter
Lively Member
Re: What is the importance of Enumerations?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|