CP.
CP.

Reputation: 155

String References in C#

this is my first post.

I am selecting some fields from a database which are numeric id values e.g. 10,20,100,110 etc. These numbers actually mean something meaningful such as area. Ideally there should be a look up database table with matching ID and name field but there isnt and it has now become difficult to implement in a reasonable timescale (politics).

I currently use a cumbesome switch function to check the id and return the relevent text.

private string GetUnitName(string areaid)
{
    string areaName = string.Empty;
    switch (areaid.Trim())
    {
        case "10":
            areaName = "area 1";
            break;
        case "20":
            areaName = "area 2";
            break;
        case "30":
            areaName = "area 3";
            break;
    }
    return areaName ;
}

I cant get them in a dateabse so what is the best way to store these area strings?
Can I group them in a settings file or something?

Thanks for listening

Upvotes: 0

Views: 197

Answers (6)

tjmoore
tjmoore

Reputation: 1084

In terms of code storage I would consider Dictionary and KeyValuePair, which makes lookup easy and quick. Assuming you're talking about a quantity of data that can be handled in memory that is.

See Best implementation for Key Value Pair Data Structure?

Then you could load the store from a file. XML would be a good way, although if it really is just plain ID and string value, then a simple CSV text file would suffice.

If you are talking a huge amount of data however then it may be better looking into a random access kind of file storage, but then the time taken to do this may be better spent with a database.

On the other hand, if it's just a handful of data items and these are guaranteed not to change, then as others have mentioned, an enum would be fine.

Upvotes: 0

rohancragg
rohancragg

Reputation: 5116

You might want to consider Enumeration Classes to give you most of the benefits of enums, but still allow you to extend the implementation as you see fit (such as adding your own method of persistence and caching as suggested in some of the other answers to this question).

There's some more info in a question I asked about enums.

Upvotes: 0

cjk
cjk

Reputation: 46415

You could store them as an enum:

public enum Areas
{
    [Description("Area 1")]
    Area1 = 10,
    [Description("Area 2")]
    Area2 = 20,
    [Description("Area 3")]
    Area3 = 30
}

Areas x = Areas.Area1;
string xName = x.ToString();

Upvotes: 4

Pete Duncanson
Pete Duncanson

Reputation: 3246

As an Enum you'd have to recompile if you ever needed to change them (depends how often they change)?

Alternatively you could just have a XML Document that you could stash in the Cache and XPath it to find the text for the id?

<Areas>
  <Area id="10">Area 1</Area>
  <Area id="20">Area 2</Area>
  <Area id="30">Area 3</Area>
</Areas>

XPath: /Areas/Area[@id='<your id here>']

Set the Cache Dependancy to expire if the file is changed then when you update it you won't have to restart anything. For speed you could read this into memory as a hash so you don't have to keep XPathing the doc all the time too.

Upvotes: 3

Adriaan Stander
Adriaan Stander

Reputation: 166346

Can you then not rather store them in an XML file and dynamically load them on startup/data refresh. This way you can create the framework for later change to a database table.

Upvotes: 1

Jimmeh
Jimmeh

Reputation: 2862

You could make an object containing the Id and the Text, and you might find better ways to refactor from there.

Edit: and store them in a List somewhere, of course :)

Upvotes: 0

Related Questions