473,387 Members | 1,876 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,387 software developers and data experts.

Load Data from Database as Class Properties or enum

Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't know
how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am wrong.

Any ideas how to go about it?

Regards,
Waqas Pitafi
Jul 21 '05 #1
3 3101
Hi Waqas,

Why would you want to create an enum at runtime?
Why don't you simply use DataTable?
And yes, if you create enum at runtime there is no way of using it at design
time.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Waqas Pitafi" <Waqas Pi****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't
know
how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am
wrong.

Any ideas how to go about it?

Regards,
Waqas Pitafi

Jul 21 '05 #2
Thanks for your quick reply.

I have a master table which is holding Status values. Based on these values
I have to perform different jobs in different classes.

e.g. Status.Read would mean to show only Read records on various controls.

Right now I have created a duplicate enum class representing my Database
Table precisely but latter if there is a new Status added or any current
record edited I will have to make adjustments to the code and recompile
application. Currently I have created an enum like this,

public enum QuestionsStatus
{
Ignore = -1,
Unread = 1,
Read = 2,
ForwardedToConsultant = 3,
ForwardedToMe = 4,
Answered = 5,
MarkedForDeletion = 6
}
This is exact representation of the data in the database table.

I hope you got it now why I need this. I don't want to hard code these
status values in my code so if 1 is edited I have to go and change it at
every place it's used.

"Miha Markic [MVP C#]" wrote:
Hi Waqas,

Why would you want to create an enum at runtime?
Why don't you simply use DataTable?
And yes, if you create enum at runtime there is no way of using it at design
time.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Waqas Pitafi" <Waqas Pi****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't
know
how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am
wrong.

Any ideas how to go about it?

Regards,
Waqas Pitafi


Jul 21 '05 #3
CodeSmtih is a perfect tool for what are you after.
www.ericjsmith.net/codesmith

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Waqas Pitafi" <Wa*********@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
Thanks for your quick reply.

I have a master table which is holding Status values. Based on these
values
I have to perform different jobs in different classes.

e.g. Status.Read would mean to show only Read records on various controls.

Right now I have created a duplicate enum class representing my Database
Table precisely but latter if there is a new Status added or any current
record edited I will have to make adjustments to the code and recompile
application. Currently I have created an enum like this,

public enum QuestionsStatus
{
Ignore = -1,
Unread = 1,
Read = 2,
ForwardedToConsultant = 3,
ForwardedToMe = 4,
Answered = 5,
MarkedForDeletion = 6
}
This is exact representation of the data in the database table.

I hope you got it now why I need this. I don't want to hard code these
status values in my code so if 1 is edited I have to go and change it at
every place it's used.

"Miha Markic [MVP C#]" wrote:
Hi Waqas,

Why would you want to create an enum at runtime?
Why don't you simply use DataTable?
And yes, if you create enum at runtime there is no way of using it at
design
time.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Waqas Pitafi" <Waqas Pi****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
> Hi,
>
> I have a table with the data like this,
>
> ID Status Description
> 1 R Read
> 2 U UnRead
> D D Deleted
>
> I want to load this data in a class (say named Status) which I want to
> use
> through out my project and should be able to access it like,
>
> Status.Read or
> Status.UnRead or
> Status.Deleted
>
> with each returning their corresponding values as in the database table
> shown above.
>
> To sum it up I want to create an enum with values loaded from database.
> I
> think it may be possible using Reflection or Custom Attributes but
> don't
> know
> how to do it.
>
> One more thing is if I use reflection than I guess I won't be able to
> use
> Status.Read in design time which means no use of it, correct me if I am
> wrong.
>
> Any ideas how to go about it?
>
> Regards,
> Waqas Pitafi


Jul 21 '05 #4

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

Similar topics

0
by: Jeff Patrick | last post by:
I have a series of class libraries. Some of the functions require string constants to be passed in in order to effect certain behavior. Over time the list of strings that could be passed in grew...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
2
by: David Batt | last post by:
Hello I have a problem whereby I wish to setup a custom tooltip. I have the code attached to do this, it all works very wel if you have the following code in a button click event on the form ...
3
by: Waqas Pitafi | last post by:
Hi, I have a table with the data like this, ID Status Description 1 R Read 2 U UnRead D D ...
4
by: CPD | last post by:
From PHP, doing a DROP TABLE and a CREATE TABLE is successful, but doing a LOAD FILE keeps failing, with the error "Access denied for user 'db_user'@'localhost' (using password: YES)". The...
8
by: Ryan Liu | last post by:
Is that a good idea to add a Hashtable to System.Windows.Forms.Application class? And we can put any user global data in it. I have some class library used by a few other applications in slight...
2
by: Michael Bray | last post by:
With the recent release of EF I've decided to dig into it a bit more than I did before... the question I'm specifically interested in, but haven't been able to find a resource to answer it is......
3
by: Queez | last post by:
Very quick question. I have a master page (M1) and three content pages (C1, C2, C3). In M1, there's a list of three anchors (A1, A2, A3) which point towards the three content pages. The...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...
0
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...
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,...

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.