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. Creating enum at runtime and showing it on a property grid

Creating enum at runtime and showing it on a property grid

Scheduled Pinned Locked Moved Visual Basic
csscomquestion
3 Posts 3 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.
  • T Offline
    T Offline
    THEMYTH
    wrote on last edited by
    #1

    Hi coders, I am planning to create an enum at runtime and set it as a property of a class. That is,

    Class MyClass
    Public Property Type As (New Created Enum)
    ....
    End Class

    For creating an enum at runtime I have used the following code

     Public Function CreateEnum() As \[Enum\]
            Dim domain As AppDomain
            domain = Threading.Thread.GetDomain
            Dim name As New System.Reflection.AssemblyName
            name.Name = "EnumCounters"
            Dim asmBuilder As System.Reflection.emit.AssemblyBuilder
            asmBuilder = domain.DefineDynamicAssembly(name, Reflection.Emit.AssemblyBuilderAccess.Run)
            Dim modBuilder As ModuleBuilder
            modBuilder = asmBuilder.DefineDynamicModule("EnumModule")
            Dim enumBuilder As EnumBuilder
            enumBuilder = modBuilder.DefineEnum("EnumCounters", TypeAttributes.Public, GetType(Integer))
    

    '----- HERE IS WHERE I ADD THE ITEMS
    For i As Integer = 0 To DiagManager.Counters.Count - 1
    enumBuilder.DefineLiteral(CType(DiagManager.Counters(i), Counter).Caption, i)
    Next
    '-----
    Dim enumType As Type = enumBuilder.CreateType
    Dim enumObj As [Enum] = System.Activator.CreateInstance(enumType)

            Return enumObj
        End Function
    

    Then, I set my property's value as the returned value of the function. For full code please see (http://emreyazici.com/CodeFormatter.htm -- site does not have banners or advertisement. I only wrote the code) Normally it would work. But when I assign the 'selectedobject' property of 'property grid' to the created instance of class, it does only display the FIRST item of enum in the property grid http://emreyazici.com/res.jpg But I want it to show all items How may I correct it? Thanks...

    Best Regards Emre YAZICI

    D T 2 Replies Last reply
    0
    • T THEMYTH

      Hi coders, I am planning to create an enum at runtime and set it as a property of a class. That is,

      Class MyClass
      Public Property Type As (New Created Enum)
      ....
      End Class

      For creating an enum at runtime I have used the following code

       Public Function CreateEnum() As \[Enum\]
              Dim domain As AppDomain
              domain = Threading.Thread.GetDomain
              Dim name As New System.Reflection.AssemblyName
              name.Name = "EnumCounters"
              Dim asmBuilder As System.Reflection.emit.AssemblyBuilder
              asmBuilder = domain.DefineDynamicAssembly(name, Reflection.Emit.AssemblyBuilderAccess.Run)
              Dim modBuilder As ModuleBuilder
              modBuilder = asmBuilder.DefineDynamicModule("EnumModule")
              Dim enumBuilder As EnumBuilder
              enumBuilder = modBuilder.DefineEnum("EnumCounters", TypeAttributes.Public, GetType(Integer))
      

      '----- HERE IS WHERE I ADD THE ITEMS
      For i As Integer = 0 To DiagManager.Counters.Count - 1
      enumBuilder.DefineLiteral(CType(DiagManager.Counters(i), Counter).Caption, i)
      Next
      '-----
      Dim enumType As Type = enumBuilder.CreateType
      Dim enumObj As [Enum] = System.Activator.CreateInstance(enumType)

              Return enumObj
          End Function
      

      Then, I set my property's value as the returned value of the function. For full code please see (http://emreyazici.com/CodeFormatter.htm -- site does not have banners or advertisement. I only wrote the code) Normally it would work. But when I assign the 'selectedobject' property of 'property grid' to the created instance of class, it does only display the FIRST item of enum in the property grid http://emreyazici.com/res.jpg But I want it to show all items How may I correct it? Thanks...

      Best Regards Emre YAZICI

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Why are you creating an Enum at runtime?? Since an Enum is pretty much only used inside your code, I fail to see the point. You might want to check to see if all the enum values are actually being created. It's possible that your only successfully creating one of them.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      1 Reply Last reply
      0
      • T THEMYTH

        Hi coders, I am planning to create an enum at runtime and set it as a property of a class. That is,

        Class MyClass
        Public Property Type As (New Created Enum)
        ....
        End Class

        For creating an enum at runtime I have used the following code

         Public Function CreateEnum() As \[Enum\]
                Dim domain As AppDomain
                domain = Threading.Thread.GetDomain
                Dim name As New System.Reflection.AssemblyName
                name.Name = "EnumCounters"
                Dim asmBuilder As System.Reflection.emit.AssemblyBuilder
                asmBuilder = domain.DefineDynamicAssembly(name, Reflection.Emit.AssemblyBuilderAccess.Run)
                Dim modBuilder As ModuleBuilder
                modBuilder = asmBuilder.DefineDynamicModule("EnumModule")
                Dim enumBuilder As EnumBuilder
                enumBuilder = modBuilder.DefineEnum("EnumCounters", TypeAttributes.Public, GetType(Integer))
        

        '----- HERE IS WHERE I ADD THE ITEMS
        For i As Integer = 0 To DiagManager.Counters.Count - 1
        enumBuilder.DefineLiteral(CType(DiagManager.Counters(i), Counter).Caption, i)
        Next
        '-----
        Dim enumType As Type = enumBuilder.CreateType
        Dim enumObj As [Enum] = System.Activator.CreateInstance(enumType)

                Return enumObj
            End Function
        

        Then, I set my property's value as the returned value of the function. For full code please see (http://emreyazici.com/CodeFormatter.htm -- site does not have banners or advertisement. I only wrote the code) Normally it would work. But when I assign the 'selectedobject' property of 'property grid' to the created instance of class, it does only display the FIRST item of enum in the property grid http://emreyazici.com/res.jpg But I want it to show all items How may I correct it? Thanks...

        Best Regards Emre YAZICI

        T Offline
        T Offline
        TwoFaced
        wrote on last edited by
        #3

        I think the problem is that your property returns a general object. Take this code for example. You'll see that the property works fine when I specifically declare it as my enumerator. However, things don't work as well if I make a simple change of declaring the property as an object. I'm not sure how to accomplish what you want but I do know this is why your having a problem.

        'Requires a propertygrid
        Public Class Form1
        Dim test As New TestEnum

        Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            PropertyGrid1.SelectedObject = test
        End Sub
        

        End Class

        Public Class TestEnum
        Dim _TestMyEnum As MyEnum

        'This works just fine because we have declared the property as MyEnum
        Public Property TestMyEnum() As MyEnum
            Get
                Return \_TestMyEnum
            End Get
            Set(ByVal value As MyEnum)
                \_TestMyEnum = value
            End Set
        End Property
        
        'This returns the exact same enumerator but has been declared as a general object
        'The same problem occurs here as you are seeing in your code
        Public Property TestMyEnumObject() As Object
            Get
                Return \_TestMyEnum
            End Get
            Set(ByVal value As Object)
                \_TestMyEnum = value
            End Set
        End Property
        
        Public Enum MyEnum
            Test0
            Test1
            Test2
        End Enum
        

        End Class

        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