472,377 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,377 software developers and data experts.

enumeration and how to populate them during runtime

I have a question about enumeration and how to populate them during
runtime.
I am using vb.net but will happily take any advice in c# as well.
Here is an example to help illustrate what I am after.

Create a class named “clsMyItems” and in that class place an enum.
Public Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
End Enum

Now from another class call the Items enum.

Dim MyItems as new clsMyItems

MyItems.Items.

After typing the last dot in “MyItems.Items.” the intellisense will show
a nice drop down list of all the enumerated items in Items. This is
exactly what I am looking for.

Now for the problem…
I don’t know ahead of time what items or how many of them will be listed
in the Items enum. This is just a simplified example of what will
eventually go into a custom control I am working on. If I can get past
this issue it will make the control insanely easy to use.

Is there a way to populate the Items enum at runtime or an equivalent
method that will give me the drop down list I so desperately need.

If it makes a difference, the control inherits from
System.ComponentModel.Component

I could do it as System.Windows.Forms.UserControl but I would prefer to
do it as a component since it will never be seen on any form.
Its primary use is for windows forms but it will be used on web forms
I’m sure.
Nov 20 '05 #1
3 10513
Sampson,

Not sure exactly what you want to do but
System.Enum.GetValues(GetType(Items)) will return an array of all the values
in Enum Items

For example, you can populate a combo box with the values of
'ProjectCategory' enumeration as follows
myCombo.DataSource = System.Enum.GetValues(GetType(ProjectCategory))

Stephen

"Sampson" <no*****@none.com> wrote in message
news:c2**********@daisy.noc.ucla.edu...
I have a question about enumeration and how to populate them during
runtime.
I am using vb.net but will happily take any advice in c# as well.
Here is an example to help illustrate what I am after.

Create a class named “clsMyItems” and in that class place an enum.
Public Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
End Enum

Now from another class call the Items enum.

Dim MyItems as new clsMyItems

MyItems.Items.

After typing the last dot in “MyItems.Items.” the intellisense will show
a nice drop down list of all the enumerated items in Items. This is
exactly what I am looking for.

Now for the problem…
I don’t know ahead of time what items or how many of them will be listed
in the Items enum. This is just a simplified example of what will
eventually go into a custom control I am working on. If I can get past
this issue it will make the control insanely easy to use.

Is there a way to populate the Items enum at runtime or an equivalent
method that will give me the drop down list I so desperately need.

If it makes a difference, the control inherits from
System.ComponentModel.Component

I could do it as System.Windows.Forms.UserControl but I would prefer to
do it as a component since it will never be seen on any form.
Its primary use is for windows forms but it will be used on web forms
I’m sure.

Nov 20 '05 #2
I'm building a custom control that when placed on a form will give me
intellisense access to the valid values for that item.

So on my form I would type something like "UserControl1.Items.(the
enumerated list would appear here)".

I don’t know what the list of items will be until a property is set on
the control so I need a way to set that enumeration up during runtime.

I have tried a reflection routine and while it appears to run, I can’t
figure out where to find the newly created enumeration...

Here’s the code I just tried.

Private Sub createPermissionEnums()
Dim currentDomain As AppDomain
Dim myAssemblyName As System.Reflection.AssemblyName
Dim myAssemblyBuilder As System.Reflection.Emit.AssemblyBuilder
Dim myModuleBuilder As System.Reflection.Emit.ModuleBuilder

' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain

' Create assembly in current currentDomain
myAssemblyName = New System.Reflection.AssemblyName
myAssemblyName.Name = "TempAssembly"

' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
currentDomain.DefineDynamicAssembly(myAssemblyName ,
System.Reflection.Emit.AssemblyBuilderAccess.Run)

' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("TempModule" )

' Define a enumeration type with name 'MyEnum' in the 'TempModule'.
Dim myEnumBuilder As System.Reflection.Emit.EnumBuilder =
myModuleBuilder.DefineEnum("MyEnum", Reflection.TypeAttributes.Public,
GetType(Integer))

' Define the named static fields in the enumeration type 'MyEnum'.
myEnumBuilder.DefineLiteral("MyEnumMember1", 2)
myEnumBuilder.DefineLiteral("MyEnumMember2", 3)
myEnumBuilder.CreateType()

End Sub


Stephen Muecke wrote:
Sampson,

Not sure exactly what you want to do but
System.Enum.GetValues(GetType(Items)) will return an array of all the values
in Enum Items

For example, you can populate a combo box with the values of
'ProjectCategory' enumeration as follows
myCombo.DataSource = System.Enum.GetValues(GetType(ProjectCategory))

Stephen

"Sampson" <no*****@none.com> wrote in message
news:c2**********@daisy.noc.ucla.edu...
I have a question about enumeration and how to populate them during
runtime.
I am using vb.net but will happily take any advice in c# as well.
Here is an example to help illustrate what I am after.

Create a class named “clsMyItems” and in that class place an enum.
Public Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
End Enum

Now from another class call the Items enum.

Dim MyItems as new clsMyItems

MyItems.Items.

After typing the last dot in “MyItems.Items.” the intellisense will show
a nice drop down list of all the enumerated items in Items. This is
exactly what I am looking for.

Now for the problem…
I don’t know ahead of time what items or how many of them will be listed
in the Items enum. This is just a simplified example of what will
eventually go into a custom control I am working on. If I can get past
this issue it will make the control insanely easy to use.

Is there a way to populate the Items enum at runtime or an equivalent
method that will give me the drop down list I so desperately need.

If it makes a difference, the control inherits from
System.ComponentModel.Component

I could do it as System.Windows.Forms.UserControl but I would prefer to
do it as a component since it will never be seen on any form.
Its primary use is for windows forms but it will be used on web forms
I’m sure.



Nov 20 '05 #3
Sampson,
Enum values are known at compile time.

It sounds like you want some sort of (readonly?) Collection object,
unfortunately Collection objects do not participate in intellisense (in that
you do not get a list of values at runtime).

You will need to decide if you want an Enum (intellisense) or a Collection
(no intellisense).

Note: you can use a TypeConverter to display the dynamic list of values from
the collection in the Property window of the designers (Component or
Control) you just cannot get the list of values in the Intellisense. Post if
you need info on using a TypeCOnverter to get the dynamic list of values in
the Property window of the designers...

Hope this helps
Jay

"Sampson" <no*****@none.com> wrote in message
news:c2**********@daisy.noc.ucla.edu...
I have a question about enumeration and how to populate them during
runtime.
I am using vb.net but will happily take any advice in c# as well.
Here is an example to help illustrate what I am after.

Create a class named “clsMyItems” and in that class place an enum.
Public Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
End Enum

Now from another class call the Items enum.

Dim MyItems as new clsMyItems

MyItems.Items.

After typing the last dot in “MyItems.Items.” the intellisense will show
a nice drop down list of all the enumerated items in Items. This is
exactly what I am looking for.

Now for the problem…
I don’t know ahead of time what items or how many of them will be listed
in the Items enum. This is just a simplified example of what will
eventually go into a custom control I am working on. If I can get past
this issue it will make the control insanely easy to use.

Is there a way to populate the Items enum at runtime or an equivalent
method that will give me the drop down list I so desperately need.

If it makes a difference, the control inherits from
System.ComponentModel.Component

I could do it as System.Windows.Forms.UserControl but I would prefer to
do it as a component since it will never be seen on any form.
Its primary use is for windows forms but it will be used on web forms
I’m sure.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
0
by: dgoel | last post by:
Hi, I Have a text file & I want to open it in excel sheet ( withou importing). I have written code for it, but it is not opening exce sheet. It opens the text file, but does not create a excel...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
7
by: Don | last post by:
I need to get the type of an enumeration from an instance of a class. e.g. Public Class MyClass Public Enum MyEnum value1 = 1 End Enum End Class
10
by: MLH | last post by:
My A97 runtime installations are sometimes paused during the install process prompting user with messages saying the files are in use. Generally, I tell them to click IGNORE. Although I haven't...
0
by: p | last post by:
I am wirting an applications where I use Roles. My roles are held in an SQL Table where the fields are simply RolID and RoleName. I am writing the Application in ASP.NET. I assign the roles to...
0
by: John Saunders | last post by:
Given a schema as follows: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">...
0
by: Rich | last post by:
Hello, I started using the Reportviewer control (very nice) for generating Reporting Services type reports in my VB2005 app. I have been experimenting using a designer Reportviewer control from...
1
by: PPA | last post by:
Hi Guys I am really new to XML/XSD and Castor. Could you please help me out here? I have following XSD: -----------------------------XSD STARTS----------------------------------- <xs:schema...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.