Set enum variable using int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Haynes

    Set enum variable using int

    I've read all the posts in this forum that I can find that look related to
    this issue and I have only found 1 solution that I consider to be a bit of a
    hack. What I want to do is assign a value to an enum variable using an int.
    What I am using right now is something like this:

    public enum MyEnum
    {
    value1,
    value2,
    value3
    };

    ....

    MyEnum var1;

    // this value actually comes from a database so I can't substitute the
    literal value
    int intVar = 1;

    var1 = (MyEnum)Enum.Pa rse(var1.GetTyp e(), intVar.ToString ());

    Is there a simpler way to do this? It doesn't seem like setting the value
    should require converting to string and then parsing and casting.
  • James Swindell

    #2
    Re: Set enum variable using int

    Have you tried just casting the integer value?

    MyEnum m = (MyEnum)1;

    "Brian Haynes" <BrianHaynes@di scussions.micro soft.com> wrote in message
    news:593F51DF-2C97-4EB8-B526-3F084FABF821@mi crosoft.com...[color=blue]
    > I've read all the posts in this forum that I can find that look related to
    > this issue and I have only found 1 solution that I consider to be a bit of
    > a
    > hack. What I want to do is assign a value to an enum variable using an
    > int.
    > What I am using right now is something like this:
    >
    > public enum MyEnum
    > {
    > value1,
    > value2,
    > value3
    > };
    >
    > ...
    >
    > MyEnum var1;
    >
    > // this value actually comes from a database so I can't substitute the
    > literal value
    > int intVar = 1;
    >
    > var1 = (MyEnum)Enum.Pa rse(var1.GetTyp e(), intVar.ToString ());
    >
    > Is there a simpler way to do this? It doesn't seem like setting the value
    > should require converting to string and then parsing and casting.[/color]


    Comment

    • MuZZy

      #3
      Re: Set enum variable using int

      Brian Haynes wrote:[color=blue]
      > I've read all the posts in this forum that I can find that look related to
      > this issue and I have only found 1 solution that I consider to be a bit of a
      > hack. What I want to do is assign a value to an enum variable using an int.
      > What I am using right now is something like this:
      >
      > public enum MyEnum
      > {
      > value1,
      > value2,
      > value3
      > };
      >
      > ...
      >
      > MyEnum var1;
      >
      > // this value actually comes from a database so I can't substitute the
      > literal value
      > int intVar = 1;
      >
      > var1 = (MyEnum)Enum.Pa rse(var1.GetTyp e(), intVar.ToString ());
      >
      > Is there a simpler way to do this? It doesn't seem like setting the value
      > should require converting to string and then parsing and casting.[/color]

      Try this:

      var1 = (MyEnum)intVar;


      It works for me

      MuZZy

      Comment

      • Gabriel Lozano-Morán

        #4
        Re: Set enum variable using int

        var1 = (myEnum)intVar;

        Gabriel Lozano-Morán


        Comment

        • Alex K.

          #5
          RE: Set enum variable using int

          use

          var1 = (MyEnum)intVar;

          and don't forget to catch the exception because the values coming from
          database may be out of your enum range.

          HTH
          Alex

          "Brian Haynes" wrote:
          [color=blue]
          > I've read all the posts in this forum that I can find that look related to
          > this issue and I have only found 1 solution that I consider to be a bit of a
          > hack. What I want to do is assign a value to an enum variable using an int.
          > What I am using right now is something like this:
          >
          > public enum MyEnum
          > {
          > value1,
          > value2,
          > value3
          > };
          >
          > ...
          >
          > MyEnum var1;
          >
          > // this value actually comes from a database so I can't substitute the
          > literal value
          > int intVar = 1;
          >
          > var1 = (MyEnum)Enum.Pa rse(var1.GetTyp e(), intVar.ToString ());
          >
          > Is there a simpler way to do this? It doesn't seem like setting the value
          > should require converting to string and then parsing and casting.[/color]

          Comment

          • Mythran

            #6
            Re: Set enum variable using int


            "Brian Haynes" <BrianHaynes@di scussions.micro soft.com> wrote in message
            news:593F51DF-2C97-4EB8-B526-3F084FABF821@mi crosoft.com...[color=blue]
            > I've read all the posts in this forum that I can find that look related to
            > this issue and I have only found 1 solution that I consider to be a bit of
            > a
            > hack. What I want to do is assign a value to an enum variable using an
            > int.
            > What I am using right now is something like this:
            >
            > public enum MyEnum
            > {
            > value1,
            > value2,
            > value3
            > };
            >
            > ...
            >
            > MyEnum var1;
            >
            > // this value actually comes from a database so I can't substitute the
            > literal value
            > int intVar = 1;
            >
            > var1 = (MyEnum)Enum.Pa rse(var1.GetTyp e(), intVar.ToString ());
            >
            > Is there a simpler way to do this? It doesn't seem like setting the value
            > should require converting to string and then parsing and casting.[/color]

            public enum MyEnum : int
            {
            Value1 = 0,
            Value2,
            Value3
            };

            MyEnum var1 = (MyEnum) 1;
            MyEnum var2 = MyEnum.Value2;
            MyEnum var3 = (MyEnum) Enum.Parse(type of(MyEnum), "Value3", true);

            The above three can be done.

            What I do, as a side note, is store the values in a database as the names of
            the enumeration value. For example, I if I had MyEnum (as in the above
            example), I would not store a value of 0, 1, or 2 into the database...I
            would store the string "Value1", "Value2", or "Value3". I would then use
            Enum.Parse() to convert the value to MyEnum. I do it this way "most of the
            time" so users don't have to "guess" what those values are when they create
            their AdHoc reports.

            Hope this helps :)

            Mythran

            Comment

            • Jon Skeet [C# MVP]

              #7
              RE: Set enum variable using int

              Alex K. <AlexK@discussi ons.microsoft.c om> wrote:[color=blue]
              > var1 = (MyEnum)intVar;
              >
              > and don't forget to catch the exception because the values coming from
              > database may be out of your enum range.[/color]

              No exception will be thrown with the above code - enums do no range
              checking.

              --
              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

              Working...