Load Data from Database as Class Properties or enum

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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
 
Back
Top