0

I am wanting to store key - value data and be able to access it in an efficient manner.

Basically: I have a custom object(EquipmentObj) and w/ in that object is a property called "DeviceType". In the constructor of the object, I am passing a string which goes out to a Dictionary (Local Variable of EquipmentObj) and returns a value if the Dictionary has the key.

In an attempt to minimize initializing the Dictionary 25 times on the heap, (EquipmentObj is instantiated 25-50 times) I am wondering if there is a more efficient way to do this.

My first thought was XML, but I can't add deserialization; I wont get into this.

My next thought was possibly using a static class. But I still need to define the KeyValuePair or Dictionary and static classes cant have instance members.

What would you all suggest?

Here is a sample of what I am basically doing right now.

class EquipmentObj
    {
        public EquipmentObj(string deviceType)
        {
            addItems(); 
            this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
        }
        public string DeviceType { get; set; }
        private Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

        private void addItems()
        {
            //Add items to Dictionary 
        }
    }
6
  • How is the dictionary populated in the first place? Commented Jan 31, 2013 at 22:04
  • A static class can have static members. But you don't need that, why don't you just make EquipmentList a static member of EquipmentObj? Commented Jan 31, 2013 at 22:04
  • That sound much like micro optimization - don't waste your time with that before it is proven to be a problem, just my advise. Commented Jan 31, 2013 at 22:04
  • 1
    @Casperah - Normally, I'd agree, and this isn't worth doing just for performance issues. But it is better design to make the dictionary static in the first place, so it's worth doing for that. Commented Jan 31, 2013 at 22:09
  • Does the EquipmentList dictionary represent the equipment included in an EquipmentObj? Or is this something else? Commented Jan 31, 2013 at 22:10

2 Answers 2

1

A static class can't have instance members, but a non-static class can have static members. You can make EquipmentList and addItems() both static without changing EquipmentObj itself.

class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        addItems(); 
        this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
    }
    public string DeviceType { get; set; }
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static void addItems()
    {
        //Add items to Dictionary 
    }
}

You'd call it as:

EquipmentObj.addItems();
Sign up to request clarification or add additional context in comments.

Comments

0

You could make a Manager class to handle the device types, Its not required but it does seperate the logic and makes accesing the devicetype from other area's in your app a bit easier.

public class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        this.DeviceType = EquipmentObjManager.GetDeviceType(deviceType);
    }
    public string DeviceType { get; set; }
}

public class EquipmentObjManager
{
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static string GetDeviceType(string deviceType)
    {
        return EquipmentList.ContainsKey(deviceType) ? EquipmentList[deviceType] : "Default";
    }

    public static void AddDeviceType(string deviceTypeKey, string deviceType)
    {
        if (!EquipmentList.ContainsKey(deviceTypeKey))
        {
            EquipmentList.Add(deviceTypeKey, deviceType);
        }
    }
}

I'm not sure where you are population you Dictionary, but you can call EquipmentObjManager.AddDeviceType to add Items to the dictionary.

May not be the best solution for you , but it could help :)

1 Comment

I am populating the dictionary, just left the items out because they are business sensitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.