Rick_Fla Posted March 17, 2004 Posted March 17, 2004 Guess this is the best place of any to put this. What are they and why/where would they be used? I am still learning VB.NET and have just taking over a small application where I work. The previous developer here used these throughout the code, which cconfused me. From what I could gather from my quick research, was they replace, or are used as booleans? Sample of how this person used it: Private Enum PID ' Field indexes for PID segment Quest_AccessionNumber = 3 Quest_ChainOfCustodyNumber = 4 Quest_PatientName = 5 Quest_AccountNumber = 18 Quest_SSN = 19 MTR_ChainOfCustodyNumber = 2 ' See subfields below MTR_AccessionNumber = 3 ' See subfields below MTR_PatientName = 6 MTR_SSN = 19 End Enum Quest and MTR are drug collection companies if that makes any difference. Quote "Nobody knows what I do until I stop doing it."
Moderators Robby Posted March 17, 2004 Moderators Posted March 17, 2004 An easy explanation for Enums would be; Let's say you had 4 columns in a table and you access each column/field by doing the following... dim myString as string = myField(1).value It can be hard to remember that the 1st index is CustomerName So the same thing can be achieved this way.... Private Enum myCustomers CustID = 0 CustName = 1 FName = 2 LName = 3 End Enum 'Then anywhere in this class you can do something like this; dim myString as string = myField(myCustomers.CustName).value Quote Visit...Bassic Software
Administrators PlausiblyDamp Posted March 17, 2004 Administrators Posted March 17, 2004 Without knowing exactly what the code in question does it is hard to justify the use of enums there. Generally enums are a nice clean way of implementing constants in your code, rather than having arbitrary numbers an enum becomes a collection of related constants. These can be passed as parameters or used as properties and the IDE will prompt you with the range of values available while the compiler will prevent values being used which are not part of the enumeration. As an example try typing in the MessageBox.Show command and as you enter parameters notice how it presents a list of options for things like buttons, icon to dispaly etc. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Gurus* Derek Stone Posted March 17, 2004 *Gurus* Posted March 17, 2004 Most would argue that enumerations are nothing more than syntactical sugar for developers. They do serve a purpose however. If the numeric value of a function's argument (or arguments) changes such that it breaks existing code you have a huge manageability problem. However if the developer isn't forced to input numeric values in the first place, and uses enumerations instead, there's no need to sift through thousands of lines of code making updates. Additionally, enumerations tend to be self-documenting: the names of the elements describe the item they reference. This is different than numeric values, which have no descriptive characteristics. Quote Posting Guidelines
Rick_Fla Posted March 17, 2004 Author Posted March 17, 2004 Robby, thanks for the explanation. Looking back through the code now makes a lot of sense now than it did before. Also I thank PlausiblyDamp and Derek Stone for your input as well. Quote "Nobody knows what I do until I stop doing it."
Joe Mamma Posted April 4, 2004 Posted April 4, 2004 Guess this is the best place of any to put this. What are they and why/where would they be used? I am still learning VB.NET and have just taking over a small application where I work. The previous developer here used these throughout the code, which cconfused me. From what I could gather from my quick research, was they replace, or are used as booleans? Sample of how this person used it: Private Enum PID ' Field indexes for PID segment Quest_AccessionNumber = 3 Quest_ChainOfCustodyNumber = 4 Quest_PatientName = 5 Quest_AccountNumber = 18 Quest_SSN = 19 MTR_ChainOfCustodyNumber = 2 ' See subfields below MTR_AccessionNumber = 3 ' See subfields below MTR_PatientName = 6 MTR_SSN = 19 End Enum Quest and MTR are drug collection companies if that makes any difference. Enums are critical!!! They enforce type safety. Don't let anyone who calls them syntactical sugar anywhere near your code!!! :eek: Use them whenever they make sense!!! in databases too!!!! Don't forget the special enums called flags!!!!! Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
*Experts* Nerseus Posted April 6, 2004 *Experts* Posted April 6, 2004 Enums are about 90% good, 10% bad. The "bad" comes when you want to use them as the underlying int value. You must use manual casting to get the int value out. This can pose problems when you want to return an enum from a WebService, for example. If you return the enum, then the auto-proxy that Visual Studio creates tries to recreate the enums for you. You can change the method to return an int, but then you have to cast back to the enum. All that's not too bad... The biggest issue I've seen is when putting an enum's int value into a DataSet. I use enums for Code Dependent values (database values that have code tied to the lookup int values), but when putting the value into a DataSet you have to beware (!) - the column will gladly accept the enum directly but when you try and use it, you'll get a runtime error if the code assumes the value is an int. This will happen whenever you use the DataAdapter to update a proc, for example. But by and large, an enum is just a related group of constants. Sometimes they're used too often - when a new set of subclasses would work better for instance. If you find you have a switch statement or series of "ifs" used more than once in code, there's a chance what you really want is a new set of objects rather than an enum. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.