Results 1 to 10 of 10

Thread: enumerators

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    enumerators

    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:

    Code:
    public enum ErrorCode : int
    {
       EnumName1 = -1;
       EnumName2 = -2;
    }
    then...


    Code:
    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?
    Last edited by Techno; Apr 27th, 2006 at 05:52 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: enumerators

    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.
    Code:
    public enum Error
    {
        BigError
        LittleError
    }
    Code:
    Hashtable errorMessages = new Hashtable();
    
    errorMessages.Add(Error.BigError, "A big error has occurred.");
    errorMessages.Add(Error.LittleError, "A little error has occurred.");
    Code:
    try
    {
        // ...
    }
    catch
    {
        MessageBox.Show(errorMessages(Error.BigError));
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: enumerators

    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:

    Code:
    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;
    }
    Last edited by Techno; Apr 27th, 2006 at 07:14 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: enumerators

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: enumerators

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: enumerators

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: enumerators

    Thanks

    i understand about the "puts a limit to the values you can use" but would that not be the same as doing:

    Code:
    private string DoGetError(int errorNo)
    {
       if (errorNo == 1)
       {
       }
       else if {....}
       else {....}
    }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: enumerators

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: enumerators

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: enumerators

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width