Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Cannot create Enum Type from Type.GetType("EnumName")

Cannot create Enum Type from Type.GetType("EnumName")

Scheduled Pinned Locked Moved Visual Basic
question
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dallas Matthews
    wrote on last edited by
    #1

    I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case. Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
    Is there something I am missing? Or does it not work with Enums? Thanks in advance, Dallas

    M D 2 Replies Last reply
    0
    • D Dallas Matthews

      I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case. Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
      Is there something I am missing? Or does it not work with Enums? Thanks in advance, Dallas

      M Offline
      M Offline
      Matthew Hazlett
      wrote on last edited by
      #2

      [Enum].GetNames(Type.GetType(myEnum))

      D 1 Reply Last reply
      0
      • M Matthew Hazlett

        [Enum].GetNames(Type.GetType(myEnum))

        D Offline
        D Offline
        Dallas Matthews
        wrote on last edited by
        #3

        Type.GetType(typeName As String) expects a string variable. Besides, I do not know what enum will be thrown to the function so I cannot give it a specific enum declaration.

        M 1 Reply Last reply
        0
        • D Dallas Matthews

          Type.GetType(typeName As String) expects a string variable. Besides, I do not know what enum will be thrown to the function so I cannot give it a specific enum declaration.

          M Offline
          M Offline
          Matthew Hazlett
          wrote on last edited by
          #4

          Sorry its just getType not type.gettype like this: objectState is a public enum. Dim tmp As String For Each tmp In [Enum].GetNames(GetType(objectState)) Console.WriteLine(tmp) Next

          D 1 Reply Last reply
          0
          • M Matthew Hazlett

            Sorry its just getType not type.gettype like this: objectState is a public enum. Dim tmp As String For Each tmp In [Enum].GetNames(GetType(objectState)) Console.WriteLine(tmp) Next

            D Offline
            D Offline
            Dallas Matthews
            wrote on last edited by
            #5

            I am not refering to the GetType operator, but to the GetType method of the Type class in the System namespace. Just type in System.Type.GetType(...) and intellisense will show you the shared method I am trying to use.

            M 1 Reply Last reply
            0
            • D Dallas Matthews

              I am not refering to the GetType operator, but to the GetType method of the Type class in the System namespace. Just type in System.Type.GetType(...) and intellisense will show you the shared method I am trying to use.

              M Offline
              M Offline
              Matthew Hazlett
              wrote on last edited by
              #6

              huh, what are you trying to do. I guess I dont see what you want to accomplish. [enum].getNames(gettype(myEnum)) will get the names in the enum [enum].getName(gettype(myEnum, myVar)) will get a name of an enmum variable dim value as myenum will create an instence of the enum (like you said in your first post)

              1 Reply Last reply
              0
              • D Dallas Matthews

                I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case. Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
                Is there something I am missing? Or does it not work with Enums? Thanks in advance, Dallas

                D Offline
                D Offline
                Dallas Matthews
                wrote on last edited by
                #7

                My bad.... Turns out the enum I was working with was declared within a class (I guess I had to leave for the weekend to figure it out). Once I moved it so it was no longer nested, it worked.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups