Click to See Complete Forum and Search --> : enumerators
Techno
Apr 27th, 2006, 05:42 PM
Hi. I am new to enumerations.
I basically want to create specific enumerators and associate them with a string error message.
how do I do this?
I have:
public enum ErrorCode : int
{
EnumName1 = -1;
EnumName2 = -2;
}
then...
public class ErrorDescriptors
{
const string EnumName1 = "error 1";
const string EnumName2 = "error 2";
}
public string DoGetErrorMessage(ErrorCode errorNumber)
{
//now I want to take the parameters and associate it with an error message matching the ErrorCode with the ErrorDescriptor class. how?
}
As well as this, is it possible to say give the method an INT and then the method would figure out what that value is equal to in the enumerators?
jmcilhinney
Apr 27th, 2006, 06:44 PM
An enumerator and an enumeration are two different things. An enumerator is an object that allows you to traverse a collection. An enumeration is a type that assigns a numerical value to each of a group of constants.
As you've posted it there is no connection between those enumerated values and those string constants, and to create one would require reflection. Let's start with the definition of the enumeration. Generally speaking there is no need or point to specify the numerical values yourself. Unless you actually need to use the values as numbers at some point, like if they are coming from another source as integers and you need to cast to your enumerated type, then there is no value added by not simply accepting the default values. The advantage to using an enumeration over plain integers is that it limits the number of valid values and it gives additional meaning by using a name instead of a number. Those names are identifiers though, not strings. If you want to associate your enumerated values with a string to be used as an error message then you'd need to use a dictionary of some sort, like a Hastable, where the key is the enumerated value and the value is the error string.public enum Error
{
BigError
LittleError
}Hashtable errorMessages = new Hashtable();
errorMessages.Add(Error.BigError, "A big error has occurred.");
errorMessages.Add(Error.LittleError, "A little error has occurred.");try
{
// ...
}
catch
{
MessageBox.Show(errorMessages(Error.BigError));
}
Techno
Apr 27th, 2006, 07:10 PM
Thanks, yeh I am aware of the whole assigned the int value to the enumerator and so on and the errors will be coming from another source.
I'll take a look into your example more :)
i basically need to define certain errors and their values
then I want to take this enum and match it with a const string and return it.
this is what i have right now:
public string DoGetErrorMessage(int theErrorCode)
{
string theErrorMessage = "";
foreach (ErrorCode currentEnum in Enum.GetValues(typeof(ErrorCode)))
{
if ((int)currentEnum == (int)theErrorCode)
{
//how to return the enum error value string?
}
}
return theErrorMessage;
}
jmcilhinney
Apr 27th, 2006, 07:35 PM
You don't need a loop. The whole point of an enumeration is that the value IS the number. If you're using a loop then the enumeration is pointless. You need to setup a relationship between the number and the string, which is exactly what I did with the Hashtable. You then simply cast the number as your enumerated type and use it as a key to get the corresponding value from the dictionary.
Techno
Apr 27th, 2006, 07:50 PM
many thanks!
ok here is another stupid Q
what would the difference be between doing that, and making a method which takes an int and returns the appropriate string?
what is the advantage/disadvantage between the 2?
jmcilhinney
Apr 27th, 2006, 08:15 PM
Not a great deal necessarily. Like I said, the enumeration uses identifiers rather than numbers so the meaning is clearer, plus it limits what values you can use. You'll still have to use a method to build the dictionary in the first place.
Techno
Apr 27th, 2006, 08:25 PM
Thanks :)
i understand about the "puts a limit to the values you can use" but would that not be the same as doing:
private string DoGetError(int errorNo)
{
if (errorNo == 1)
{
}
else if {....}
else {....}
}
jmcilhinney
Apr 27th, 2006, 09:04 PM
But you can still pass any integer vaue to your method. If the method argument is declared as your enumerated type then you can only pass valid values to the method in the first place instead of the method having to catch invalid values. Of course, you may actually want the method to catch unknown error numbers and return a generic error message in those cases.
Techno
Apr 28th, 2006, 06:40 AM
I guess I'm confusing myself again.
Enums are the way to go obviously, instead of the old school Win32 API error codes.
Obviously if the error source is within the application, then use enum's but if it is outside of the application, i.e custom made SQL Error, then you have no choice but to handle it any other way. thats all good.....
but say when we get the enum value (i.e someEnum.InvalidConnection) and return it back to the caller, how would the caller handle the value?
jmcilhinney
Apr 28th, 2006, 07:45 PM
The identifier is just an alias for a numerical value. If you mouse over it in your code Intellisense will show what integer value it is equivalent to. You can assign it anywhere an integer is required simply by casting it. So if you're getting an integer error code from outside your app you would cast it as your enumerated type and then use it as such throughout your code, then cast it back to an integer when you need to pass it out of your app again, like to save in a database or as a parameter to an external method.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.