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
 
Try using ObjectSpaces due in Whidbey and ADO.NET 2.0 providing object to
database mapping which is what i beleive you are looking for.

HTH
rawCoder
 
Waqas,

You say you want to use this enum throughout your project however for what.

What is not Unread, what is Deleted, what is Read.

After that it is maybe to tell how you can do that.

Cor
 
Waqas,

The answer is simple and again starting with a question, why do you have
that status in so many places as I understand while it is always the same.

Just set it in one place and use it.
In a module or in a property from a shared class.

When you do not know how to do the last, reply.

I hope this helps?

Cor
 
I guess I am not able to explain it properly. I will try it again,

see,

Generally speaking these Status values should be fixed then I can hardcode
them in my code but that's not the case. If a new Status is included in the
system, I have to add necessary code for that.

Now why do I have to change the code? See the following code sample.

// New questions
DataListBind(ref dlNew, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatus.Unread));

// Waiting For Answer
DataListBind(ref dlRead, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatus.Read));

// Archive
DataListBind(ref dlArchive, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatus.Answered));


While this is my QuestionsStatus enum
public enum QuestionsStatus
{
Ignore = -1,
Unread = 1,
Read = 2,
ForwardedToConsultant = 3,
ForwardedToMe = 4,
Answered = 5,
MarkedForDeletion = 6
}

which I have created as a duplicate for my table data.

Now say we add a new Status in the table called 'NeverDeleteMe', I have to
return to this enum and add the new Status and recompile my code.

Hope now you get my problem.
 
You could store the status values in a XML config file and then write a
routine to update the XML file whenever a new status value is added.

Hope this helps.
 
Waqas,

You show exactly what I tried to say in my previous question. And I try to
show you, that you are probably making it yourself very difficult.

QuestionStatuscurrent = 1
Or when you want to use the enum in that case
QuestionStatuscurrent = QuestionStatus.read
// New questions
DataListBind(ref dlNew, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatuscurrent));

// Waiting For Answer
DataListBind(ref dlRead, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatuscurent));

// Archive
DataListBind(ref dlArchive, new
QuestionsSystem(UserName).CategorySummary(QuestionsStatuscurrent));
And when you have this in more programs, make a shared variable from
QuestionStatusCurrent either using a module or nice with a shared class and
a property.

Cor

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top