Connecting Tech Pros Worldwide Forums | Help | Site Map

Load Data from Database as Class Properties or enum

Waqas Pitafi
Guest
 
Posts: n/a
#1: Nov 22 '05
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

Miha Markic [MVP C#]
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Load Data from Database as Class Properties or enum


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 Pitafi@discussions.microsoft.com> wrote in message
news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@microsoft.com...[color=blue]
> 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[/color]


Waqas Pitafi
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Load Data from Database as Class Properties or enum


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:
[color=blue]
> 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 Pitafi@discussions.microsoft.com> wrote in message
> news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@microsoft.com...[color=green]
> > 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[/color]
>
>
>[/color]
Miha Markic [MVP C#]
Guest
 
Posts: n/a
#4: Nov 22 '05

re: Load Data from Database as Class Properties or enum


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" <WaqasPitafi@discussions.microsoft.com> wrote in message
news:BBC9756F-9194-409D-B0ED-73AC664FE7E7@microsoft.com...[color=blue]
> 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:
>[color=green]
>> 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 Pitafi@discussions.microsoft.com> wrote in message
>> news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@microsoft.com...[color=darkred]
>> > 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[/color]
>>
>>
>>[/color][/color]


Closed Thread