473,803 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13328
> 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.Reflecti on.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuild er;
ModuleBuilder myModuleBuilder ;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName. Name = "MyAssembly ";
myAssemblyBuild er =
AppDomain.Curre ntDomain.Define DynamicAssembly (myAssemblyName ,
AssemblyBuilder Access.Run);
myModuleBuilder = myAssemblyBuild er.DefineDynami cModule("MyModu le");
myEnumBuilder =
myModuleBuilder .DefineEnum("My Namespace.Count ries",TypeAttri butes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.D efineLiteral("N orway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.D efineLiteral("U nited Kingdom",
2);
myEnumBuilder.C reateType();

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(type of(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**********@o bjectware.no> wrote in message
news:OU******** ******@tk2msftn gp13.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.Reflecti on.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuild er;
ModuleBuilder myModuleBuilder ;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName. Name = "MyAssembly ";
myAssemblyBuild er =
AppDomain.Curre ntDomain.Define DynamicAssembly (myAssemblyName ,
AssemblyBuilder Access.Run);
myModuleBuilder = myAssemblyBuild er.DefineDynami cModule("MyModu le");
myEnumBuilder =
myModuleBuilder .DefineEnum("My Namespace.Count ries",TypeAttri butes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.D efineLiteral("N orway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.D efineLiteral("U nited
Kingdom", 2);
myEnumBuilder.C reateType();

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.Reflecti on.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuild er;
ModuleBuilder myModuleBuilder ;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName. Name = "MyAssembly ";
myAssemblyBuild er =
AppDomain.Curre ntDomain.Define DynamicAssembly (myAssemblyName ,
AssemblyBuilder Access.Run);
myModuleBuilder = myAssemblyBuild er.DefineDynami cModule("MyModu le");
myEnumBuilder =
myModuleBuilder .DefineEnum("My Namespace.Count ries",TypeAttri butes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.D efineLiteral("N orway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.D efineLiteral("U nited Kingdom",
2);
myEnumBuilder.C reateType();

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.Activato r 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
269
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 DoSomething(ByVal value() as System.Enum) ' I know this doesn't work, but... In the case of a single object I want to extract its value, and in the case of an array of enums (any type of Enum) I want to create a joined string containing all of their...
2
3989
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 ordinary Array with the index numbers. Answers can come be direct, or simple links to data access explanations on websites or even references to good books about data access and the reason to use different data types in different situations. I...
10
9097
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 .... const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX}; RWCString MESSAGES; <--------------------- MESSAGES = "MESSAGE_A";
3
16078
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 the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
5
5866
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 = "MyEnum.One"; // This doesn't work (obviously), but effectively this
9
2380
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 Derived(), New Derived(), New Derived()}
15
8451
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 a={int,float,char,int*..............................}, so that a should return me int and a should return me
8
2125
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 command word) which might be int or double or char (again defined by the command word). For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might be followed by the command byte STRING would be followed by a...
4
1446
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. --------------------- class Program { enum Foo
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.