Cast from int to constants possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Bass

    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_INSUFFICI ENT_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)(nErr orCode)).ToStri ng();

    Thanks!


  • Jon Skeet [C# MVP]

    #2
    Re: Cast from int to constants possible?

    <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue]
    > 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_INSUFFICI ENT_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)(nErr orCode)).ToStri ng();[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Daniel Bass

      #3
      Re: Cast from int to constants possible?

      [color=blue]
      > No. Instead of having consts, use an enumeration instead - then you get
      > type safety *and* you can go back to the name easily.[/color]

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

      Should've put "ClassOfConstan tsGivenToMeThat ICan'tAlter" ! ;o)

      Agree that enums are far superior.

      Thanks.
      Dan.



      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Cast from int to constants possible?

        <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue][color=green]
        > > No. Instead of having consts, use an enumeration instead - then you get
        > > type safety *and* you can go back to the name easily.[/color]
        >
        > I would, but the class of constants isn't mine, stupid for me to put
        > "MyClass"!! !
        >
        > Should've put "ClassOfConstan tsGivenToMeThat ICan'tAlter" ! ;o)
        >
        > Agree that enums are far superior.[/color]

        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 - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Daniel Bass

          #5
          Re: Cast from int to constants possible?


          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]" <skeet@pobox.co m> wrote in message
          news:MPG.1a240c 8450eb82c5989ac c@msnews.micros oft.com...
          <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue][color=green]
          > > No. Instead of having consts, use an enumeration instead - then you get
          > > type safety *and* you can go back to the name easily.[/color]
          >
          > I would, but the class of constants isn't mine, stupid for me to put
          > "MyClass"!! !
          >
          > Should've put "ClassOfConstan tsGivenToMeThat ICan'tAlter" ! ;o)
          >
          > Agree that enums are far superior.[/color]

          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 - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too


          Comment

          • 100

            #6
            Re: Cast from int to constants possible?

            [color=blue]
            > Agree that enums are far superior.[/color]
            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.Notebo ok;}
            }
            }

            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_INSUFFIC IENT_DISK_SPACE ";
            break;
            default:
            res = "UNKNOWN ERROR";
            break;
            }
            return res;
            }
            }

            Now you can write

            string errMsg = "Error! Code: " + ((MyNewClass)nE rrorCode).ToStr ing();

            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


            Comment

            Working...