473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Array of string to Enum types

Hi guy, i just wondering that is it posible to convert the array of string to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show me
the way to do it
Thank you very much
Thomas
Nov 16 '05 #1
4 13309
> Hi guy, i just wondering that is it posible to convert the array of string
to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show
me
the way to do it
Thank you very much
Thomas


You can generate an Enum on the fly by using the methods in
System.Reflection.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuilder;
ModuleBuilder myModuleBuilder;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "MyAssembly";
myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAs semblyName,
AssemblyBuilderAccess.Run);
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule");
myEnumBuilder =
myModuleBuilder.DefineEnum("MyNamespace.Countries" ,TypeAttributes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("Norway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("United Kingdom",
2);
myEnumBuilder.CreateType();

You can use this code as a boiler plate for your dynamic country enum.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #2
Assuming you have defined an Enum of countries, you can do:
Country country = (Country) Enum.Parse(typeof(Country), countryString,
true);

This will however require that the country names in the data table matches
those defined in the enum.

Another way might be to store an integer in the database representing the
country,
which is then associated with the member of the enum that uses that
particular value.
This would also be a lot more efficient.

Country country = (Country) countryInteger;

But the preferred approach really depends on how, exactly, the country
names/identifiers is stored in the database.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Anders Norås [MCAD]" <an**********@objectware.no> wrote in message
news:OU**************@tk2msftngp13.phx.gbl...
Hi guy, i just wondering that is it posible to convert the array of
string to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show
me
the way to do it
Thank you very much
Thomas


You can generate an Enum on the fly by using the methods in
System.Reflection.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuilder;
ModuleBuilder myModuleBuilder;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "MyAssembly";
myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAs semblyName,
AssemblyBuilderAccess.Run);
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule");
myEnumBuilder =
myModuleBuilder.DefineEnum("MyNamespace.Countries" ,TypeAttributes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("Norway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("United
Kingdom", 2);
myEnumBuilder.CreateType();

You can use this code as a boiler plate for your dynamic country enum.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #3
Thank you for your information however could you please also show me how to
apply this in the following situation

Assume i have the method which return the country name
[WebMethod]
public string GetCountryName(CountryType type) {}

The CountryType is the enum type which getting from country names from the
database

How can i allow user to use the CountryType that i've created on the fly.

"Anders Norås [MCAD]" wrote:
Hi guy, i just wondering that is it posible to convert the array of string
to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show
me
the way to do it
Thank you very much
Thomas


You can generate an Enum on the fly by using the methods in
System.Reflection.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuilder;
ModuleBuilder myModuleBuilder;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "MyAssembly";
myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAs semblyName,
AssemblyBuilderAccess.Run);
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule");
myEnumBuilder =
myModuleBuilder.DefineEnum("MyNamespace.Countries" ,TypeAttributes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("Norway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("United Kingdom",
2);
myEnumBuilder.CreateType();

You can use this code as a boiler plate for your dynamic country enum.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #4
> Assume i have the method which return the country name
[WebMethod]
public string GetCountryName(CountryType type) {}

The CountryType is the enum type which getting from country names from the
database
You can't use dynamically created types with web services because the types
your web service returns must accept and return types defined in the WSDL
for the web service.

From what I can understand from your problem description and the code above
you want your method to accept a list of codes (e.g. ISO language codes such
as NO and GB) and return a list of full names from a database? If this is
the case, an enum is not the appropriate type to use, you should use an list
instead.
For example:
public string GetCountryName(ArrayList countries) {}

An enum is a type where a list of named constants can be defined. Unless the
enum is marked with the Flags attribute (which is used for bitwise flags) an
enum instance can only represent one constant at a time.
How can i allow user to use the CountryType that i've created on the fly.

You can create new instances of dynamically created types using bysing the
System.Activator class.

Anders Norås
http://dotnetjunkies.com/weblog/anoras
Nov 16 '05 #5

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

Similar topics

0
by: Jon the Blind | last post by:
I'm trying to create a procedure overload that accepts an array of Enum values, of any enum type. Take this example: Overloads Sub DoSomething(ByVal value as Object) Overloads Sub...
2
by: Ben Enfield | last post by:
I have an application where the data access portion will return a value based on the enum type month passed to it. I was wondering if this should be done with a hash map (dictionary) or an...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
3
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like...
5
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 =...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
4
by: Rene | last post by:
If variance/covariance is not allowed in array of value types, why is the expression on the "Main" method run successfully (see snippet below)? I am missing something? Thank you. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.