Search

Jul 15, 2008

Overloading ToString Method of Enum

Hello All,

The general scenario for using enum is to provide a way to restrict a variable to one of a fixed set of values.

Now at the same time we need the string value of enum, and by default we get what we right as the name of enum, like I created following enum

public enum Operation
{
ChangePassword,
EditProfile,
SearchResult,
Search
}

Now when I try to call ToString method will show the name of enum.

Operation operation = Operation.ChangePassword;
Console.Write(operation.ToString());

//It will show you
//ChangePassword

Now what if I want to show Change Password insted of ChangePassword?

We can do it by Extension methods which are introduced in C# 3.0, I created following Extension method which operates on my Operation enum. Here is the method.

public static string EnumToString(this Operation operation)
{
switch (operation)
{
case Operation.ChangePassword:
return "Change Password";
case Operation.EditProfile:
return "Edit Profile";
case Operation.SearchResultL:
return "Search Result";
default :
return operation.ToString();
}
}
You see the switch case? I write the case for value which needs to show differently then name.

Operation operation = Operation.ChangePassword;
Console.Write(operation.EnumToString());

//this will show you what you needed
//Change Password

2 comments:

Anonymous said...

im a desperate student trying to self-teach java, and im getting royally frustrated with this whole concept. it would be amazing if you could help me work out why i am getting an error on this from my ide. if you choose to have mercy and help a poor student in need…thank you!!!

public enum Type {NOT, EQUI, ISO, SCA};
public Type typeOfTri;

public void Decisions(int side1, int side2, int
side3)
{
if (side1 + side2 < side3 || side2 + side3
< side1 || side3 + side1 < side2) {
typeOfTri = Type.NOT;
}
else if (side1 == side2 && side1 ==
side3){
typeOfTri = Type.EQUI;
}
else if (side1 == side2 || side1 == side3 || side3 == side2){
typeOfTri = Type.ISO;
}
else {
typeOfTri = Type.SCA;
}

public String toString()
{
switch (typeOfTri)
{
case NOT:
return "It's not a triangle.";
break;
case EQUI:
return "It's an equilateral triangle.";
break;
case ISO:
return "It's an isosceles traingle.";
break;
case SCA:
return "It's a scalene triangle.";
break;
}
}

my error message is: This method must return a result of type String. (It also says my break statements are unreachable code…?)

again, if you choose to help i will be eternally in your debt…

Imran said...

Hi Michey,

I seen your code and found a very silly mistake you did.

There is default case which is missing.

See your function must return string, In switch case you handel all the possibity, but switch is having one default clase that needs to be there and also return string. You know that there are only 4 option to Type but SWITCH clause does not! So it looks for default case which is missing.

So there is two solution, 1, you add default and return a string, or at the end of function you write return statement.

I made post with regards to .NET Framework, but I think it does not matter which language it is. I am sure this clears your doubt and also solve your problem.