开发者

Need to have some method to access data in my class

I have the following class:

public static class ReferenceData
{

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" },
            };
    }
}

When I want to find the name of the datastore given the value I have been using:

DatastoreText = ReferenceData.GetDatastore().Single(s => s.Value == datastoreValue).Text

This works good but is there a better way? Can I code the above into my reference class and so re开发者_开发百科use it in different places?

Thanks


Just put that code in a method with a value types property...

public static string GetDatastoreText(string datastoreValue)
{
    return ReferenceData.GetDatastore().Single(s => s.Value == datastoreValue).Text;
}

or am I missing something here?


Use IDictionary<TKey, TValue> to make it faster and surround access in method like musefan said.


You are creating and returning a new array each time you access the property. Is this really your intention (seeing that you initialize all data in code)? If not, you should use a private field for your data.

Also, you could use a Dictionary for your data, so you could access it by the Value like this:

ReferenceData.GetDataStore()[datastoreValue]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜