Popular Posts

Feb 17, 2011

USING ENUM(C#)


In your professional or student life you have written down some sort of code snippet as follows:

Sample 1 (C# Code):

            string employeeType = employeeTypeComboBox.Text;
            if (employeeType == "Permanent")
            {
                CalculateSalary();
            }
            else if (employeeType == "Worker")
            {
                CalculateWages();
            }
            else
            {
                // Do nothing
            }

Sample 2 (C# Code):

            string studentType = studentTypeComboBox.Text;
            if (studentType == "Regular")
            {
                // Do Something
            }
            else if (studentType == "Private")
            {
                // Do Something
            }
            else
            {
                // Do nothing
            }


In these two samples, user input is taken in a string valriable (employeeType /studentType) from a ComboBox and compare the variable with some discrete hard-coded string values (Permanent/Worker/Regular/Private).

What will happen if you type (some of you has faced these sad experiences)  Parmanent instead of Permanent in Sample 2? Definitely CalculateSalary() method will be not called and even program doesn’t show you any error message (any compile error).  In best case, You will find out this problem during development and fix it immediately. But in worst case this problem arises after demployment L.

Is there any solution where my type will be detected earliar and show me a compile error?

Yes. Try emun.
See Sample Code 3, the more smart version of Sample code 1. Here you have no typo option J

Sample Code 3:

  enum EmployeeType
        {
            Permanent,
            Worker
        }

  string employeeType = employeeTypeComboBox.Text;
        if (employeeType == EmployeeType.Permanent.ToString())
        {
            CalculateSalary();
        }
        else if (employeeType == EmployeeType.Worker.ToString())
        {
            CalculateWages();
        }
        else
        {
             //Do Nothing
        }


So, why enum?
a)      To improve code clarity
b)      Make the code easier to maintain
c)      Getting error at earlier stage


Note:
a)      You can define enum inside or outside of class, but not inside of method or property.
b)      The list of names contained by a particutar type of enum called enumerator list.

More About enum:
a)      If you want you can keep value with each name in enumerator list. Example:

        enum Priority
        {
            Critical = 1,
            Important = 2,
Medium = 3,
Low = 4
  };

Also get this value by type casting to the related type.

int priorityValue = (int) Priority.Medium;

b)      Another nice thing, you can easily iterate through enumerator list.

foreach(Priority priority in Enum.GetValues(typeof(Priority)))
         {
             MessageBox.Show(priority.ToString());
   }

      or even

         for (Priority prio = Priority.Low; prio >= Priority.Critical; prio--)
         {
             MessageBox.Show(prio.ToString());
   }


Practice:

Write a program where a ComboBox contains days of a week. User will select a day and press Information Button.

If Saturday or Wednesday is selected (s)he will get the information: “Today you have a three hours long OOP class :-)”

If Sunday or Tueday is selected (s)he will get the information: “Today you have a six hours long OOP class :-)”

If Friday, Monday or Thursday is selected (s)he will get the information: “Today you have no OOP class :-(”



No comments:

Post a Comment