473,388 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Cast from int to constants possible?

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!
Nov 15 '05 #1
5 4868
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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.

Nov 15 '05 #3
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.


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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

It's IBM's MQSeries .Net interface.
(
http://www-1.ibm.com/support/docview...&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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.


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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
100
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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
12
by: Ralf | last post by:
If you want to explicitly state the type of an integer constant, you basically have two options. Either you use an explicit cast, as in: (unsigned int) 1234 or you use the UL suffixes, as in:...
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.