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 
        }
    }
Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • How is the dictionary populated in the first place? – Bobson Jan 31 '13 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? – Jordan Kaye Jan 31 '13 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. – Casperah Jan 31 '13 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. – Bobson Jan 31 '13 at 22:09
  • Does the EquipmentList dictionary represent the equipment included in an EquipmentObj? Or is this something else? – Jeff Siver Jan 31 '13 at 22:10
  • Thanks guys I have what I need. Making the Dictionary static was a perfect solution. – Botonomous Jan 31 '13 at 22:19

2 Answers2

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();
Bobson
  • 13,498
  • 5
  • 55
  • 80
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 :)

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110