How to get Enum type using Enum name?

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

    How to get Enum type using Enum name?

    How do I get an Enum's type using only the Enum name?

    e.g.

    Dim enumType as System.Type
    Dim enumName as String = "MyEnum"

    enumType = ???(enumName)


    I've tried using Type.GetType(en umName) to retrieve an Enum's type by name,
    but I always get the value Nothing in return.

    - Don


  • steve

    #2
    Re: How to get Enum type using Enum name?

    not sure i follow, don...

    all enums only have integer/numeric members. perhaps you mean something
    else?


    "Don" <unknown@oblivi on.com> wrote in message
    news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
    | How do I get an Enum's type using only the Enum name?
    |
    | e.g.
    |
    | Dim enumType as System.Type
    | Dim enumName as String = "MyEnum"
    |
    | enumType = ???(enumName)
    |
    |
    | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
    name,
    | but I always get the value Nothing in return.
    |
    | - Don
    |
    |


    Comment

    • Don

      #3
      Re: How to get Enum type using Enum name?

      ?

      I don't understand what the possible values of an Enum has to do with
      getting a Type object for an Enum.


      "steve" <a@b.com> wrote in message news:MoD4e.1415 $AF6.312@fe04.l ga...[color=blue]
      > not sure i follow, don...
      >
      > all enums only have integer/numeric members. perhaps you mean something
      > else?
      >
      >
      > "Don" <unknown@oblivi on.com> wrote in message
      > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
      > | How do I get an Enum's type using only the Enum name?
      > |
      > | e.g.
      > |
      > | Dim enumType as System.Type
      > | Dim enumName as String = "MyEnum"
      > |
      > | enumType = ???(enumName)
      > |
      > |
      > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
      > name,
      > | but I always get the value Nothing in return.
      > |
      > | - Don
      > |
      > |
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to get Enum type using Enum name?

        "Don" <unknown@oblivi on.com> schrieb:[color=blue]
        > How do I get an Enum's type using only the Enum name?
        >
        > e.g.
        >
        > Dim enumType as System.Type
        > Dim enumName as String = "MyEnum"
        >
        > enumType = ???(enumName)
        >
        >
        > I've tried using Type.GetType(en umName) to retrieve an Enum's type by
        > name,
        > but I always get the value Nothing in return.[/color]


        Make sure you fully qualify the type name correctly. You can determine a
        type's full name by using 'GetType(<type name>).FullName '.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • steve

          #5
          Re: How to get Enum type using Enum name?

          the quandry, of course, is that the type of object of an enum is...enum. the
          possible values of the members of an enum were not what i commented on, but
          the fact that the members themselves are all, and always, integer values.

          hence, to me, you question is a bit vexing...an enum object is an enum type
          and its members are integers.

          could you be more specific is what it is you'd like to discover about an
          enum?


          "Don" <unknown@oblivi on.com> wrote in message
          news:osD4e.9084 11$6l.111545@pd 7tw2no...
          | ?
          |
          | I don't understand what the possible values of an Enum has to do with
          | getting a Type object for an Enum.
          |
          |
          | "steve" <a@b.com> wrote in message news:MoD4e.1415 $AF6.312@fe04.l ga...
          | > not sure i follow, don...
          | >
          | > all enums only have integer/numeric members. perhaps you mean something
          | > else?
          | >
          | >
          | > "Don" <unknown@oblivi on.com> wrote in message
          | > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
          | > | How do I get an Enum's type using only the Enum name?
          | > |
          | > | e.g.
          | > |
          | > | Dim enumType as System.Type
          | > | Dim enumName as String = "MyEnum"
          | > |
          | > | enumType = ???(enumName)
          | > |
          | > |
          | > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
          | > name,
          | > | but I always get the value Nothing in return.
          | > |
          | > | - Don
          | > |
          | > |
          | >
          | >
          |
          |


          Comment

          • steve

            #6
            Re: How to get Enum type using Enum name?

            lemme guess, you have an function/sub/property param that could be one of
            many enum's and you're trying to decide, by name, which to use for
            conversion/validation/action to preform?

            is it that or something similar?


            "Don" <unknown@oblivi on.com> wrote in message
            news:osD4e.9084 11$6l.111545@pd 7tw2no...
            | ?
            |
            | I don't understand what the possible values of an Enum has to do with
            | getting a Type object for an Enum.
            |
            |
            | "steve" <a@b.com> wrote in message news:MoD4e.1415 $AF6.312@fe04.l ga...
            | > not sure i follow, don...
            | >
            | > all enums only have integer/numeric members. perhaps you mean something
            | > else?
            | >
            | >
            | > "Don" <unknown@oblivi on.com> wrote in message
            | > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
            | > | How do I get an Enum's type using only the Enum name?
            | > |
            | > | e.g.
            | > |
            | > | Dim enumType as System.Type
            | > | Dim enumName as String = "MyEnum"
            | > |
            | > | enumType = ???(enumName)
            | > |
            | > |
            | > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
            | > name,
            | > | but I always get the value Nothing in return.
            | > |
            | > | - Don
            | > |
            | > |
            | >
            | >
            |
            |


            Comment

            • Don

              #7
              Re: How to get Enum type using Enum name?

              When you create an Enum, you create a new Type. Any variable that is
              declared as an Enum has that Enum as it's type. A variable is not of type
              Integer when declared as an Enum you've created. The System.Type class even
              has a property call IsEnum. I've written other code that retrieves all
              nested types of a class, and Enums declared in the class are returned as
              Types. I know you can get the Type of an Enum. I just can't seem to get it
              to work when I'm just passing the name of the Enum to Type.GetType().

              I am trying to determine all the members of an Enum, given only the Enum's
              name. That is, I want to get the Enum's Type so I can use the
              Enum.GetNames() or Enum.GetValues( ) method to retrieve a list of all of its
              members. I don't know how else to put it.



              "steve" <a@b.com> wrote in message news:hXD4e.6115 $HC3.5488@fe07. lga...[color=blue]
              > the quandry, of course, is that the type of object of an enum is...enum.[/color]
              the[color=blue]
              > possible values of the members of an enum were not what i commented on,[/color]
              but[color=blue]
              > the fact that the members themselves are all, and always, integer values.
              >
              > hence, to me, you question is a bit vexing...an enum object is an enum[/color]
              type[color=blue]
              > and its members are integers.
              >
              > could you be more specific is what it is you'd like to discover about an
              > enum?
              >
              >
              > "Don" <unknown@oblivi on.com> wrote in message
              > news:osD4e.9084 11$6l.111545@pd 7tw2no...
              > | ?
              > |
              > | I don't understand what the possible values of an Enum has to do with
              > | getting a Type object for an Enum.
              > |
              > |
              > | "steve" <a@b.com> wrote in message news:MoD4e.1415 $AF6.312@fe04.l ga...
              > | > not sure i follow, don...
              > | >
              > | > all enums only have integer/numeric members. perhaps you mean[/color]
              something[color=blue]
              > | > else?
              > | >
              > | >
              > | > "Don" <unknown@oblivi on.com> wrote in message
              > | > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
              > | > | How do I get an Enum's type using only the Enum name?
              > | > |
              > | > | e.g.
              > | > |
              > | > | Dim enumType as System.Type
              > | > | Dim enumName as String = "MyEnum"
              > | > |
              > | > | enumType = ???(enumName)
              > | > |
              > | > |
              > | > | I've tried using Type.GetType(en umName) to retrieve an Enum's type[/color]
              by[color=blue]
              > | > name,
              > | > | but I always get the value Nothing in return.
              > | > |
              > | > | - Don
              > | > |
              > | > |
              > | >
              > | >
              > |
              > |
              >
              >[/color]


              Comment

              • Don

                #8
                Re: How to get Enum type using Enum name?

                Hmmm... What you suggest works, but only if the Enum is declared in the same
                project. If I try to do the same thing with an Enum declared in a
                referenced dll, then the Type.GetType() method returns Nothing.

                This is the code I am executing:

                Public Enum abc
                val1 = 1
                val2 = 2
                End Enum

                Dim s As String
                s = GetType(abc).Fu llName

                Dim t As Type
                t = Type.GetType(s) ' <--- Works!

                This works fine. However, if, say, the "abc" Enum existed in a dll that I
                was referencing, the code does not work:

                ' (assuming "abc" is a publicly declared Enum in a public module called
                ' "Enumeratio ns" in the referenced class library "OtherDLL")
                Dim s As String
                s = GetType(OtherDL L.Enumerations. abc).FullName

                Dim t As Type
                t = Type.GetType(s) ' <--- Fails!


                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:O6CDFniOFH A.2468@tk2msftn gp13.phx.gbl...[color=blue]
                > "Don" <unknown@oblivi on.com> schrieb:[color=green]
                > > How do I get an Enum's type using only the Enum name?
                > >
                > > e.g.
                > >
                > > Dim enumType as System.Type
                > > Dim enumName as String = "MyEnum"
                > >
                > > enumType = ???(enumName)
                > >
                > >
                > > I've tried using Type.GetType(en umName) to retrieve an Enum's type by
                > > name,
                > > but I always get the value Nothing in return.[/color]
                >
                >
                > Make sure you fully qualify the type name correctly. You can determine a
                > type's full name by using 'GetType(<type name>).FullName '.
                >
                > --
                > M S Herfried K. Wagner
                > M V P <URL:http://dotnet.mvps.org/>
                > V B <URL:http://classicvb.org/petition/>
                >[/color]


                Comment

                • Don

                  #9
                  Re: How to get Enum type using Enum name?

                  I just discovered that the following code works:

                  ' (assuming "abc" is a publicly declared Enum in a public module called
                  ' "Enumeratio ns" in the referenced class library "OtherDLL")
                  Dim s As String
                  s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName '<--- NOT
                  FullName

                  Dim t As Type
                  t = Type.GetType(s) ' <--- Works!

                  Thanks for pointing me into the right direction.

                  - Don


                  "Don" <unknown@oblivi on.com> wrote in message
                  news:ZrR4e.9147 68$8l.831814@pd 7tw1no...[color=blue]
                  > Hmmm... What you suggest works, but only if the Enum is declared in the[/color]
                  same[color=blue]
                  > project. If I try to do the same thing with an Enum declared in a
                  > referenced dll, then the Type.GetType() method returns Nothing.
                  >
                  > This is the code I am executing:
                  >
                  > Public Enum abc
                  > val1 = 1
                  > val2 = 2
                  > End Enum
                  >
                  > Dim s As String
                  > s = GetType(abc).Fu llName
                  >
                  > Dim t As Type
                  > t = Type.GetType(s) ' <--- Works!
                  >
                  > This works fine. However, if, say, the "abc" Enum existed in a dll that I
                  > was referencing, the code does not work:
                  >
                  > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                  called[color=blue]
                  > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                  > Dim s As String
                  > s = GetType(OtherDL L.Enumerations. abc).FullName
                  >
                  > Dim t As Type
                  > t = Type.GetType(s) ' <--- Fails!
                  >
                  >
                  > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  > news:O6CDFniOFH A.2468@tk2msftn gp13.phx.gbl...[color=green]
                  > > "Don" <unknown@oblivi on.com> schrieb:[color=darkred]
                  > > > How do I get an Enum's type using only the Enum name?
                  > > >
                  > > > e.g.
                  > > >
                  > > > Dim enumType as System.Type
                  > > > Dim enumName as String = "MyEnum"
                  > > >
                  > > > enumType = ???(enumName)
                  > > >
                  > > >
                  > > > I've tried using Type.GetType(en umName) to retrieve an Enum's type by
                  > > > name,
                  > > > but I always get the value Nothing in return.[/color]
                  > >
                  > >
                  > > Make sure you fully qualify the type name correctly. You can determine[/color][/color]
                  a[color=blue][color=green]
                  > > type's full name by using 'GetType(<type name>).FullName '.
                  > >
                  > > --
                  > > M S Herfried K. Wagner
                  > > M V P <URL:http://dotnet.mvps.org/>
                  > > V B <URL:http://classicvb.org/petition/>
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Don

                    #10
                    Re: How to get Enum type using Enum name?

                    Duh. I forgot that I will only have the name of the Enum as a string to
                    start with. It doesn't look like I'll be able to do this because the
                    AssembylQualifi edName -- which is what I need to start off with -- contains
                    version information which may change, so it cannot be hardcoded into my
                    project. Argh!


                    "Don" <unknown@oblivi on.com> wrote in message
                    news:IAR4e.9157 74$6l.884013@pd 7tw2no...[color=blue]
                    > I just discovered that the following code works:
                    >
                    > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                    called[color=blue]
                    > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                    > Dim s As String
                    > s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName '<---[/color]
                    NOT[color=blue]
                    > FullName
                    >
                    > Dim t As Type
                    > t = Type.GetType(s) ' <--- Works!
                    >
                    > Thanks for pointing me into the right direction.
                    >
                    > - Don
                    >
                    >
                    > "Don" <unknown@oblivi on.com> wrote in message
                    > news:ZrR4e.9147 68$8l.831814@pd 7tw1no...[color=green]
                    > > Hmmm... What you suggest works, but only if the Enum is declared in the[/color]
                    > same[color=green]
                    > > project. If I try to do the same thing with an Enum declared in a
                    > > referenced dll, then the Type.GetType() method returns Nothing.
                    > >
                    > > This is the code I am executing:
                    > >
                    > > Public Enum abc
                    > > val1 = 1
                    > > val2 = 2
                    > > End Enum
                    > >
                    > > Dim s As String
                    > > s = GetType(abc).Fu llName
                    > >
                    > > Dim t As Type
                    > > t = Type.GetType(s) ' <--- Works!
                    > >
                    > > This works fine. However, if, say, the "abc" Enum existed in a dll that[/color][/color]
                    I[color=blue][color=green]
                    > > was referencing, the code does not work:
                    > >
                    > > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                    > called[color=green]
                    > > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                    > > Dim s As String
                    > > s = GetType(OtherDL L.Enumerations. abc).FullName
                    > >
                    > > Dim t As Type
                    > > t = Type.GetType(s) ' <--- Fails!
                    > >
                    > >
                    > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                    > > news:O6CDFniOFH A.2468@tk2msftn gp13.phx.gbl...[color=darkred]
                    > > > "Don" <unknown@oblivi on.com> schrieb:
                    > > > > How do I get an Enum's type using only the Enum name?
                    > > > >
                    > > > > e.g.
                    > > > >
                    > > > > Dim enumType as System.Type
                    > > > > Dim enumName as String = "MyEnum"
                    > > > >
                    > > > > enumType = ???(enumName)
                    > > > >
                    > > > >
                    > > > > I've tried using Type.GetType(en umName) to retrieve an Enum's type[/color][/color][/color]
                    by[color=blue][color=green][color=darkred]
                    > > > > name,
                    > > > > but I always get the value Nothing in return.
                    > > >
                    > > >
                    > > > Make sure you fully qualify the type name correctly. You can[/color][/color][/color]
                    determine[color=blue]
                    > a[color=green][color=darkred]
                    > > > type's full name by using 'GetType(<type name>).FullName '.
                    > > >
                    > > > --
                    > > > M S Herfried K. Wagner
                    > > > M V P <URL:http://dotnet.mvps.org/>
                    > > > V B <URL:http://classicvb.org/petition/>
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Don

                      #11
                      Re: How to get Enum type using Enum name?

                      Success! To do what I want to do I will simply need to know one more thing:
                      the assembly the Enum is declared in. Here is a sample of code that works
                      (I am typing this in manually, so it's a little different from my executing
                      code):

                      ' Known information about Enum
                      Dim assemblyName as String = "OtherDLL"
                      Dim enumName as String = "OtherDLL.Enume rations+abc" ' Note the "+"

                      Dim ax As System.Reflecti on.Assembly = _
                      System.Reflecti on.Assembly.Loa d(assemblyName)
                      Dim tx As Type = ax.GetType(enum Name) ' <--- Works!

                      Now I can get what I am looking for using the following code:

                      Dim a1 As Array
                      a1 = [Enum].GetNames(tx)


                      - Don


                      "Don" <unknown@oblivi on.com> wrote in message
                      news:IDR4e.9148 49$8l.462977@pd 7tw1no...[color=blue]
                      > Duh. I forgot that I will only have the name of the Enum as a string to
                      > start with. It doesn't look like I'll be able to do this because the
                      > AssembylQualifi edName -- which is what I need to start off with --[/color]
                      contains[color=blue]
                      > version information which may change, so it cannot be hardcoded into my
                      > project. Argh!
                      >
                      >
                      > "Don" <unknown@oblivi on.com> wrote in message
                      > news:IAR4e.9157 74$6l.884013@pd 7tw2no...[color=green]
                      > > I just discovered that the following code works:
                      > >
                      > > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                      > called[color=green]
                      > > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                      > > Dim s As String
                      > > s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName '<---[/color]
                      > NOT[color=green]
                      > > FullName
                      > >
                      > > Dim t As Type
                      > > t = Type.GetType(s) ' <--- Works!
                      > >
                      > > Thanks for pointing me into the right direction.
                      > >
                      > > - Don
                      > >
                      > >
                      > > "Don" <unknown@oblivi on.com> wrote in message
                      > > news:ZrR4e.9147 68$8l.831814@pd 7tw1no...[color=darkred]
                      > > > Hmmm... What you suggest works, but only if the Enum is declared in[/color][/color][/color]
                      the[color=blue][color=green]
                      > > same[color=darkred]
                      > > > project. If I try to do the same thing with an Enum declared in a
                      > > > referenced dll, then the Type.GetType() method returns Nothing.
                      > > >
                      > > > This is the code I am executing:
                      > > >
                      > > > Public Enum abc
                      > > > val1 = 1
                      > > > val2 = 2
                      > > > End Enum
                      > > >
                      > > > Dim s As String
                      > > > s = GetType(abc).Fu llName
                      > > >
                      > > > Dim t As Type
                      > > > t = Type.GetType(s) ' <--- Works!
                      > > >
                      > > > This works fine. However, if, say, the "abc" Enum existed in a dll[/color][/color][/color]
                      that[color=blue]
                      > I[color=green][color=darkred]
                      > > > was referencing, the code does not work:
                      > > >
                      > > > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                      > > called[color=darkred]
                      > > > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                      > > > Dim s As String
                      > > > s = GetType(OtherDL L.Enumerations. abc).FullName
                      > > >
                      > > > Dim t As Type
                      > > > t = Type.GetType(s) ' <--- Fails!
                      > > >
                      > > >
                      > > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                      > > > news:O6CDFniOFH A.2468@tk2msftn gp13.phx.gbl...
                      > > > > "Don" <unknown@oblivi on.com> schrieb:
                      > > > > > How do I get an Enum's type using only the Enum name?
                      > > > > >
                      > > > > > e.g.
                      > > > > >
                      > > > > > Dim enumType as System.Type
                      > > > > > Dim enumName as String = "MyEnum"
                      > > > > >
                      > > > > > enumType = ???(enumName)
                      > > > > >
                      > > > > >
                      > > > > > I've tried using Type.GetType(en umName) to retrieve an Enum's type[/color][/color]
                      > by[color=green][color=darkred]
                      > > > > > name,
                      > > > > > but I always get the value Nothing in return.
                      > > > >
                      > > > >
                      > > > > Make sure you fully qualify the type name correctly. You can[/color][/color]
                      > determine[color=green]
                      > > a[color=darkred]
                      > > > > type's full name by using 'GetType(<type name>).FullName '.
                      > > > >
                      > > > > --
                      > > > > M S Herfried K. Wagner
                      > > > > M V P <URL:http://dotnet.mvps.org/>
                      > > > > V B <URL:http://classicvb.org/petition/>
                      > > > >
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Don

                        #12
                        Re: How to get Enum type using Enum name?

                        And an additional tip to those following in my footsteps: You can easily get
                        the assembly name from the full name of the Enum using the following bit of
                        code:

                        assemblyName = enumName.Substr ing(0, InStr(enumName, ".") - 1)

                        Use this and you don't need to separately store the assembly name.

                        - Don


                        "Don" <unknown@oblivi on.com> wrote in message
                        news:0LR4e.9119 87$Xk.128619@pd 7tw3no...[color=blue]
                        > Success! To do what I want to do I will simply need to know one more[/color]
                        thing:[color=blue]
                        > the assembly the Enum is declared in. Here is a sample of code that works
                        > (I am typing this in manually, so it's a little different from my[/color]
                        executing[color=blue]
                        > code):
                        >
                        > ' Known information about Enum
                        > Dim assemblyName as String = "OtherDLL"
                        > Dim enumName as String = "OtherDLL.Enume rations+abc" ' Note the "+"
                        >
                        > Dim ax As System.Reflecti on.Assembly = _
                        > System.Reflecti on.Assembly.Loa d(assemblyName)
                        > Dim tx As Type = ax.GetType(enum Name) ' <--- Works!
                        >
                        > Now I can get what I am looking for using the following code:
                        >
                        > Dim a1 As Array
                        > a1 = [Enum].GetNames(tx)
                        >
                        >
                        > - Don
                        >
                        >
                        > "Don" <unknown@oblivi on.com> wrote in message
                        > news:IDR4e.9148 49$8l.462977@pd 7tw1no...[color=green]
                        > > Duh. I forgot that I will only have the name of the Enum as a string to
                        > > start with. It doesn't look like I'll be able to do this because the
                        > > AssembylQualifi edName -- which is what I need to start off with --[/color]
                        > contains[color=green]
                        > > version information which may change, so it cannot be hardcoded into my
                        > > project. Argh!
                        > >
                        > >
                        > > "Don" <unknown@oblivi on.com> wrote in message
                        > > news:IAR4e.9157 74$6l.884013@pd 7tw2no...[color=darkred]
                        > > > I just discovered that the following code works:
                        > > >
                        > > > ' (assuming "abc" is a publicly declared Enum in a public module[/color]
                        > > called[color=darkred]
                        > > > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                        > > > Dim s As String
                        > > > s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName[/color][/color][/color]
                        '<---[color=blue][color=green]
                        > > NOT[color=darkred]
                        > > > FullName
                        > > >
                        > > > Dim t As Type
                        > > > t = Type.GetType(s) ' <--- Works!
                        > > >
                        > > > Thanks for pointing me into the right direction.
                        > > >
                        > > > - Don
                        > > >
                        > > >
                        > > > "Don" <unknown@oblivi on.com> wrote in message
                        > > > news:ZrR4e.9147 68$8l.831814@pd 7tw1no...
                        > > > > Hmmm... What you suggest works, but only if the Enum is declared in[/color][/color]
                        > the[color=green][color=darkred]
                        > > > same
                        > > > > project. If I try to do the same thing with an Enum declared in a
                        > > > > referenced dll, then the Type.GetType() method returns Nothing.
                        > > > >
                        > > > > This is the code I am executing:
                        > > > >
                        > > > > Public Enum abc
                        > > > > val1 = 1
                        > > > > val2 = 2
                        > > > > End Enum
                        > > > >
                        > > > > Dim s As String
                        > > > > s = GetType(abc).Fu llName
                        > > > >
                        > > > > Dim t As Type
                        > > > > t = Type.GetType(s) ' <--- Works!
                        > > > >
                        > > > > This works fine. However, if, say, the "abc" Enum existed in a dll[/color][/color]
                        > that[color=green]
                        > > I[color=darkred]
                        > > > > was referencing, the code does not work:
                        > > > >
                        > > > > ' (assuming "abc" is a publicly declared Enum in a public module
                        > > > called
                        > > > > ' "Enumeratio ns" in the referenced class library "OtherDLL")
                        > > > > Dim s As String
                        > > > > s = GetType(OtherDL L.Enumerations. abc).FullName
                        > > > >
                        > > > > Dim t As Type
                        > > > > t = Type.GetType(s) ' <--- Fails!
                        > > > >
                        > > > >
                        > > > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in[/color][/color][/color]
                        message[color=blue][color=green][color=darkred]
                        > > > > news:O6CDFniOFH A.2468@tk2msftn gp13.phx.gbl...
                        > > > > > "Don" <unknown@oblivi on.com> schrieb:
                        > > > > > > How do I get an Enum's type using only the Enum name?
                        > > > > > >
                        > > > > > > e.g.
                        > > > > > >
                        > > > > > > Dim enumType as System.Type
                        > > > > > > Dim enumName as String = "MyEnum"
                        > > > > > >
                        > > > > > > enumType = ???(enumName)
                        > > > > > >
                        > > > > > >
                        > > > > > > I've tried using Type.GetType(en umName) to retrieve an Enum's[/color][/color][/color]
                        type[color=blue][color=green]
                        > > by[color=darkred]
                        > > > > > > name,
                        > > > > > > but I always get the value Nothing in return.
                        > > > > >
                        > > > > >
                        > > > > > Make sure you fully qualify the type name correctly. You can[/color]
                        > > determine[color=darkred]
                        > > > a
                        > > > > > type's full name by using 'GetType(<type name>).FullName '.
                        > > > > >
                        > > > > > --
                        > > > > > M S Herfried K. Wagner
                        > > > > > M V P <URL:http://dotnet.mvps.org/>
                        > > > > > V B <URL:http://classicvb.org/petition/>
                        > > > > >
                        > > > >
                        > > > >
                        > > >
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • steve

                          #13
                          Re: How to get Enum type using Enum name?

                          hmmm...

                          so you're saying that when you create an enum, you are actually creating an
                          object who's base class is enum? such that:

                          enum foo
                          bar = 0
                          end enum

                          dim e as foo
                          if typeof e is foo then return true

                          would not throw an error and neither would:

                          dim e as object = foo
                          if typeof e is enum then return true

                          both would return true...that about right? perhaps, i just haven't had need
                          to discover that nuance yet. i have seen in this ng code that does exactly
                          what you're wanting to do. have you googled yet?


                          "Don" <unknown@oblivi on.com> wrote in message
                          news:qnR4e.9157 21$6l.12993@pd7 tw2no...
                          | When you create an Enum, you create a new Type. Any variable that is
                          | declared as an Enum has that Enum as it's type. A variable is not of type
                          | Integer when declared as an Enum you've created. The System.Type class
                          even
                          | has a property call IsEnum. I've written other code that retrieves all
                          | nested types of a class, and Enums declared in the class are returned as
                          | Types. I know you can get the Type of an Enum. I just can't seem to get
                          it
                          | to work when I'm just passing the name of the Enum to Type.GetType().
                          |
                          | I am trying to determine all the members of an Enum, given only the Enum's
                          | name. That is, I want to get the Enum's Type so I can use the
                          | Enum.GetNames() or Enum.GetValues( ) method to retrieve a list of all of
                          its
                          | members. I don't know how else to put it.
                          |
                          |
                          |
                          | "steve" <a@b.com> wrote in message news:hXD4e.6115 $HC3.5488@fe07. lga...
                          | > the quandry, of course, is that the type of object of an enum is...enum.
                          | the
                          | > possible values of the members of an enum were not what i commented on,
                          | but
                          | > the fact that the members themselves are all, and always, integer
                          values.
                          | >
                          | > hence, to me, you question is a bit vexing...an enum object is an enum
                          | type
                          | > and its members are integers.
                          | >
                          | > could you be more specific is what it is you'd like to discover about an
                          | > enum?
                          | >
                          | >
                          | > "Don" <unknown@oblivi on.com> wrote in message
                          | > news:osD4e.9084 11$6l.111545@pd 7tw2no...
                          | > | ?
                          | > |
                          | > | I don't understand what the possible values of an Enum has to do with
                          | > | getting a Type object for an Enum.
                          | > |
                          | > |
                          | > | "steve" <a@b.com> wrote in message news:MoD4e.1415 $AF6.312@fe04.l ga...
                          | > | > not sure i follow, don...
                          | > | >
                          | > | > all enums only have integer/numeric members. perhaps you mean
                          | something
                          | > | > else?
                          | > | >
                          | > | >
                          | > | > "Don" <unknown@oblivi on.com> wrote in message
                          | > | > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
                          | > | > | How do I get an Enum's type using only the Enum name?
                          | > | > |
                          | > | > | e.g.
                          | > | > |
                          | > | > | Dim enumType as System.Type
                          | > | > | Dim enumName as String = "MyEnum"
                          | > | > |
                          | > | > | enumType = ???(enumName)
                          | > | > |
                          | > | > |
                          | > | > | I've tried using Type.GetType(en umName) to retrieve an Enum's type
                          | by
                          | > | > name,
                          | > | > | but I always get the value Nothing in return.
                          | > | > |
                          | > | > | - Don
                          | > | > |
                          | > | > |
                          | > | >
                          | > | >
                          | > |
                          | > |
                          | >
                          | >
                          |
                          |


                          Comment

                          • Don

                            #14
                            Re: How to get Enum type using Enum name?

                            This is the solution to my problem (which I posted in a different branch of
                            my original thread):

                            ' Known information about Enum
                            Dim assemblyName as String = "OtherDLL"
                            Dim enumName as String = "OtherDLL.Enume rations+abc"

                            Dim ax As System.Reflecti on.Assembly = _
                            System.Reflecti on.Assembly.Loa d(assemblyName)
                            Dim tx As Type = ax.GetType(enum Name)

                            What's stored in the tx variable is the result I was seeking. I wanted to
                            get the type of an Enum using just a string containing the Enum's name.
                            This is how to do it (when the Enum is in a different assembly). It boils
                            down to this:

                            <Assembly>.GetT ype(<EnumNameAs String>)

                            - Don


                            "steve" <a@b.com> wrote in message news:KKV4e.16$g y6.4@fe05.lga.. .[color=blue]
                            > hmmm...
                            >
                            > so you're saying that when you create an enum, you are actually creating[/color]
                            an[color=blue]
                            > object who's base class is enum? such that:
                            >
                            > enum foo
                            > bar = 0
                            > end enum
                            >
                            > dim e as foo
                            > if typeof e is foo then return true
                            >
                            > would not throw an error and neither would:
                            >
                            > dim e as object = foo
                            > if typeof e is enum then return true
                            >
                            > both would return true...that about right? perhaps, i just haven't had[/color]
                            need[color=blue]
                            > to discover that nuance yet. i have seen in this ng code that does exactly
                            > what you're wanting to do. have you googled yet?
                            >
                            >
                            > "Don" <unknown@oblivi on.com> wrote in message
                            > news:qnR4e.9157 21$6l.12993@pd7 tw2no...
                            > | When you create an Enum, you create a new Type. Any variable that is
                            > | declared as an Enum has that Enum as it's type. A variable is not of[/color]
                            type[color=blue]
                            > | Integer when declared as an Enum you've created. The System.Type class
                            > even
                            > | has a property call IsEnum. I've written other code that retrieves all
                            > | nested types of a class, and Enums declared in the class are returned as
                            > | Types. I know you can get the Type of an Enum. I just can't seem to[/color]
                            get[color=blue]
                            > it
                            > | to work when I'm just passing the name of the Enum to Type.GetType().
                            > |
                            > | I am trying to determine all the members of an Enum, given only the[/color]
                            Enum's[color=blue]
                            > | name. That is, I want to get the Enum's Type so I can use the
                            > | Enum.GetNames() or Enum.GetValues( ) method to retrieve a list of all of
                            > its
                            > | members. I don't know how else to put it.
                            > |
                            > |
                            > |
                            > | "steve" <a@b.com> wrote in message news:hXD4e.6115 $HC3.5488@fe07. lga...
                            > | > the quandry, of course, is that the type of object of an enum[/color]
                            is...enum.[color=blue]
                            > | the
                            > | > possible values of the members of an enum were not what i commented[/color]
                            on,[color=blue]
                            > | but
                            > | > the fact that the members themselves are all, and always, integer
                            > values.
                            > | >
                            > | > hence, to me, you question is a bit vexing...an enum object is an enum
                            > | type
                            > | > and its members are integers.
                            > | >
                            > | > could you be more specific is what it is you'd like to discover about[/color]
                            an[color=blue]
                            > | > enum?
                            > | >
                            > | >
                            > | > "Don" <unknown@oblivi on.com> wrote in message
                            > | > news:osD4e.9084 11$6l.111545@pd 7tw2no...
                            > | > | ?
                            > | > |
                            > | > | I don't understand what the possible values of an Enum has to do[/color]
                            with[color=blue]
                            > | > | getting a Type object for an Enum.
                            > | > |
                            > | > |
                            > | > | "steve" <a@b.com> wrote in message[/color]
                            news:MoD4e.1415 $AF6.312@fe04.l ga...[color=blue]
                            > | > | > not sure i follow, don...
                            > | > | >
                            > | > | > all enums only have integer/numeric members. perhaps you mean
                            > | something
                            > | > | > else?
                            > | > | >
                            > | > | >
                            > | > | > "Don" <unknown@oblivi on.com> wrote in message
                            > | > | > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
                            > | > | > | How do I get an Enum's type using only the Enum name?
                            > | > | > |
                            > | > | > | e.g.
                            > | > | > |
                            > | > | > | Dim enumType as System.Type
                            > | > | > | Dim enumName as String = "MyEnum"
                            > | > | > |
                            > | > | > | enumType = ???(enumName)
                            > | > | > |
                            > | > | > |
                            > | > | > | I've tried using Type.GetType(en umName) to retrieve an Enum's[/color]
                            type[color=blue]
                            > | by
                            > | > | > name,
                            > | > | > | but I always get the value Nothing in return.
                            > | > | > |
                            > | > | > | - Don
                            > | > | > |
                            > | > | > |
                            > | > | >
                            > | > | >
                            > | > |
                            > | > |
                            > | >
                            > | >
                            > |
                            > |
                            >
                            >[/color]


                            Comment

                            Working...