tags:

views:

50

answers:

2

I already googled around to find a solution for my need, with no success.

Let's say I've a table that looks like this:

ID |KeyId |Name |Description
1 |153 |Currency |XXXXXXXX
2 |68 |Signature |YYYYYYYY
3 |983 |Contact |ZZZZZZZZ .

Now I want to access theses values not by a collection, because I cannot remember all the values, let's say for the name.

So this is not what I want: Values.Where(v => v.Name == "Currency").Select(v => v.KeyId);

The table content changes rarely but still it is not a nice solution having a struct with all "Names" and getting the KeyId like this.

struct Values
{
    public static int Currency
    {
        get { return GetKeyId("Currency"); }
    }
}

I'm looking for a solution that creates me automatically properties out of this table. So that I can access the KeyId with intellisense. As you have for Resources in ASP.NET. There the class is automatically updated as soon as you add a new entry in the RESX file.

For example: Values.Currency , this gives me back the corresponding KeyId.

Thanks for reply

A: 

I believe this feature has been added in C# 4.0 (not yet released), announced at the Microsoft PDC. However, even that solution may not give you Intellisense for anything you haven't already typed explicitly (because if you think about it, it would be impossible for Intellisense to help you type what values might be added in the future).

Until then, the standard practice is to use a Dictionary and/or Enum (but the Dictionary will require you typing a string, and the Enum won't update automatically, or both using an Enum as the key to the dictionary to get intellisence help).

Andy Jacobs
Sounds good to me, let's see if 4.0 will have that feature.Of course, nothing can be added to intellisense what is not typed somewhere. But it would be nice to have the same behaviour as you have for ressource files (RESX). I don't know if that's an easy part. I'll try to find out and give an update here.thanks for your reply
Kosta
A: 

I found the perfect solution for me => T4 Templates

With this feature I can iterate through my table and create properties for each row, pritty nice.

There you go:
Example how to work with T4
T4 Editor - Free
Infos to T4 Editor

Kosta