Cast from int to constants possible?

  • Thread starter Thread starter Daniel Bass
  • Start date Start date
D

Daniel Bass

I've got some class which contains loads of static const int values, is
there a way that, given an int, i can quickly cast the int back to a string
representation of the const?

for eg.

MyClass contains
const int ERROR_NONE = 1
const int ERROR_NO_DRIVE = 2
const int ERROR_INSUFFICIENT_DISK_SPACE = 3
....
etc.

and given an int value of say 3, I'm able to display an error message saying
something like:

string errMsg = "Error! Code: " + ((MyClass)(nErrorCode)).ToString();

Thanks!
 
I've got some class which contains loads of static const int values, is
there a way that, given an int, i can quickly cast the int back to a string
representation of the const?

for eg.

MyClass contains
const int ERROR_NONE = 1
const int ERROR_NO_DRIVE = 2
const int ERROR_INSUFFICIENT_DISK_SPACE = 3
....
etc.

and given an int value of say 3, I'm able to display an error message saying
something like:

string errMsg = "Error! Code: " + ((MyClass)(nErrorCode)).ToString();

No. Instead of having consts, use an enumeration instead - then you get
type safety *and* you can go back to the name easily.
 
No. Instead of having consts, use an enumeration instead - then you get
type safety *and* you can go back to the name easily.

I would, but the class of constants isn't mine, stupid for me to put
"MyClass"!!!

Should've put "ClassOfConstantsGivenToMeThatICan'tAlter" ! ;o)

Agree that enums are far superior.

Thanks.
Dan.
 
I would, but the class of constants isn't mine, stupid for me to put
"MyClass"!!!

Should've put "ClassOfConstantsGivenToMeThatICan'tAlter" ! ;o)

Agree that enums are far superior.

I'd define an enumeration with the same values, and use that everywhere
you can. Where you can't, just cast from the int to the enum type. Does
that help? The only problem is what happens if the set of errors
changes - do you get new versions of this class on a regular basis?
 
It's IBM's MQSeries .Net interface.
(
http://www-1.ibm.com/support/docvie...=swg24004732&loc=en_US&cs=utf-8&cc=us&lang=en )

I prefer MSMQ for messages, IBM's version is too clever and mucks up every
time.

So when you a catch an expection, you ask for the message and it gives you
back something like " Code: 2, Reason: 2018", which might be something like
invalid queue handle, but there's no way of easily checking that.

If i press F12 to check the definition, it brings up the object explorer for
the class which doesn't say what the values of each const int is.

Thanks for your time.
Dan.

I would, but the class of constants isn't mine, stupid for me to put
"MyClass"!!!

Should've put "ClassOfConstantsGivenToMeThatICan'tAlter" ! ;o)

Agree that enums are far superior.

I'd define an enumeration with the same values, and use that everywhere
you can. Where you can't, just cast from the int to the enum type. Does
that help? The only problem is what happens if the set of errors
changes - do you get new versions of this class on a regular basis?
 
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
 
Back
Top