Agree that enums are far superior.
Not always, though.
Imagin that we have class hierarchy for Items let say
public enum ItemType{Pencil, Notebook}
public class abstract Item
{
public abstract ItemType Type
{
get;
}
}
class Pencil: Item
{
public override ItemType Type
{
get{return ItemType.Pencil;}
}
}
class Notebook: Item
{
public override ItemType Type
{
get{return ItemType.Notebook;}
}
}
If you use enums for the type as I do. You cannot extend the hierarchy with
new Items because you cannot inherit the enumeration.
But if we had
public class abstract Item
{
public abstract int Type
{
get;
}
}
and
public class ItemType
{
public const int Pencil = 1;
public const int NoteBook = 2;
}
So, we could do
class MyItemTypes: ItemType
{
public const int Staple = 3;
}
What you can do in your case is
class MyNewClass: MyClass
{
private int mErrCode;
public static explicit operator MyNewClass(int errCode)
{
MyNewClass res = new MyNewClass();
res.mErrCode = errCode;
return res;
}
public override string ToString()
{
string res = "";
switch(mErrCode)
{
case 1:
res = "ERROR_NONE";
break;
case 2:
res = "ERROR_NO_DRIVE";
break;
case 3:
res = "ERROR_INSUFFICIENT_DISK_SPACE";
break;
default:
res = "UNKNOWN ERROR";
break;
}
return res;
}
}
Now you can write
string errMsg = "Error! Code: " + ((MyNewClass)nErrorCode).ToString();
or
string errMsg = "Error! Code: " + ((MyNewClass)2).ToString();
Ofcourse with enums it would be easier, but since you don't have control
over it this will work for you.
HTH
B\rgds
100