C# Typecast to Enum from Request.Params

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpecialKay
    New Member
    • Mar 2008
    • 109

    C# Typecast to Enum from Request.Params

    Sorry if this is not the place for C#.

    in short:

    public enum MyEnum
    {
    One,
    Two,
    Three
    };

    page_load
    {
    MyEnum num = (MyEnum)Request .Params("number ");
    }

    assume the page is being called

    www.mypage.com? number="One"

    i get a simple "Error 7 Cannot convert type 'string' to 'MyEnum' .... "

    Thanks in advance
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Try converting "number" to System.Int32 then cast it as an enum.

    Nathan

    Comment

    • SpecialKay
      New Member
      • Mar 2008
      • 109

      #3
      Dont see how that would change anything. But i tried it anyway.
      Same problem, unable to convert.

      Comment

      • SpecialKay
        New Member
        • Mar 2008
        • 109

        #4
        Originally posted by SpecialKay
        Sorry if this is not the place for C#.

        in short:

        public enum MyEnum
        {
        One,
        Two,
        Three
        };

        page_load
        {
        MyEnum num = (MyEnum)Request .Params("number ");
        }

        assume the page is being called

        www.mypage.com? number="One"

        i get a simple "Error 7 Cannot convert type 'string' to 'MyEnum' .... "

        Thanks in advance


        My solution:
        Im sure there is a better way!

        Here is my solution. Im sure there is a better way.

        page_load
        {
        myEnum num = setState(Reques t.Params("numbe r");
        }

        private myEnum setState(string state)
        {
        switch(state)
        {
        case One:
        return myEnum.One;

        etc.
        }
        }

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I forget where it is found (System.Enum or something?) That will give you an enum[] type deal that lets you itterate through your ENUMs like that, then you can compare the enum's .ToString(), which in your case is "one" "two" "three", and do a string comparison to deterimine if it's your enum.

          See this post for more on enum enumeration:

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Is this what you need?
            Code:
            string s = "FirstItem"
            //Assume you have an enumeration called "MyEnum", of which each of the entries are FirstItem = 1; SecondItem = 2 etc etc.  You could find the item called "FirstItem" using the string and System.Enum.Parse...
            MyEnum SelectedEnum = System.Enum.Parse(GetType(MyEnum), s)

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              Looks like it might work, i will try it out and let you know, thanks a bunch!

              Comment

              • SpecialKay
                New Member
                • Mar 2008
                • 109

                #8
                [QUOTE=SpecialKa y]My solution:
                Im sure there is a better way!

                Here is my solution. Im sure there is a better way.

                page_load
                {
                myEnum num = setState(Reques t.Params("numbe r");
                }

                Thanks guys, you pointed me down the right path
                the end result is

                [code=c]

                public void results(httpReq uest Request)
                {
                string state = request.params["number"];

                myEnum num = (myEnum)System. Enum.Parse(type of(myEnum), state)
                }

                [/code]

                Comment

                • balabaster
                  Recognized Expert Contributor
                  • Mar 2007
                  • 798

                  #9
                  [QUOTE=SpecialKa y]
                  Originally posted by SpecialKay
                  My solution:
                  Im sure there is a better way!

                  Here is my solution. Im sure there is a better way.

                  page_load
                  {
                  myEnum num = setState(Reques t.Params("numbe r");
                  }

                  Thanks guys, you pointed me down the right path
                  the end result is

                  [code=c]

                  public void results(httpReq uest Request)
                  {
                  string state = request.params["number"];

                  myEnum num = (myEnum)System. Enum.Parse(type of(myEnum), state)
                  }

                  [/code]
                  Oops, looks like my VB to C# translation skills are slipping... hehe. At least you got it sorted ;o)

                  GetType = typeof <committing to memory...>

                  Comment

                  Working...