Enums

Rick_Fla

Well-known member
Joined
Nov 19, 2003
Messages
188
Location
Melbourne, Florida
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:
Code:
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.
 
An easy explanation for Enums would be;

Lets say you had 4 columns in a table and you access each column/field by doing the following...
Code:
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....

Code:
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
 
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.
 
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 functions argument (or arguments) changes such that it breaks existing code you have a huge manageability problem. However if the developer isnt forced to input numeric values in the first place, and uses enumerations instead, theres 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.
 
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.
 
Rick_Fla said:
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:
Code:
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.

Dont let anyone who calls them syntactical sugar anywhere near your code!!! :eek:

Use them whenever they make sense!!!
in databases too!!!!

Dont forget the special enums called flags!!!!!
 
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 thats not too bad...

The biggest issue Ive seen is when putting an enums 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, youll 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 theyre 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, theres a chance what you really want is a new set of objects rather than an enum.

-nerseus
 
Back
Top