Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a keyvaluepair with a value two different data types?

Tags:

c#

.net

winforms

I wrote a method that returns

List<KeyValuePair<CommandType, List<string>>>

CommandType is of Type enum

public enum CommandType
{
    Programmed,
    Manual
}

My issue is that the value in the KeyValuePair sometimes is an enum, and sometimes it is a list of strings, but I need to keep all the KeyValuePair in one list.

currently, I pass the value as an object in the keyvaluepair and when the method returns the list and an iterate through it, based on the key, I cast the value back to its original type.

Is there a better way to implement this?

here is a sample code

public enum  ProgrammedCommands
{
    Sntp,
    Snmp,
}
private List<KeyValuePair<CommandType, object>> GetCommandsFromTemplate(string[] templateLines)
{
    var list = new List<KeyValuePair<CommandType, object>>();

    if (templateLines != null)
        for (int lineIndex = 0; lineIndex < templateLines.Length; lineIndex++)
        {
            if (templateLines[lineIndex].Contains("!*") && templateLines[lineIndex].Contains("*!"))
            {
                KeyValuePair<CommandType, object> ProgrammedSetting;
                List<string> programmedCommandList;
                if (templateLines[lineIndex].Contains("SNTP - SNTP Server Commands"))
                {

                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Programmed, ProgrammedCommands.Sntp);
                    list.Add(ProgrammedSetting);
                }

                else if (templateLines[lineIndex].Contains("MANUAL"))
                {
                    lineIndex++;
                    List<string> manual = new List<string>();
                    while (true)
                    {
                        if (lineIndex >= templateLines.Length)
                            break;
                        if (templateLines[lineIndex].Contains("!!["))
                            lineIndex++;
                        else if (templateLines[lineIndex].Contains("]!!"))
                            break;
                        else
                        {
                            manual.Add(templateLines[lineIndex]);
                            lineIndex++;
                        }
                    }
                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Manual, manual);
                    list.Add(ProgrammedSetting);
                }
            }
        }
    return list;
}
like image 560
user3726459 Avatar asked Oct 30 '22 11:10

user3726459


1 Answers

If you want to use a single storage for different types, since the type of values can be determined only at run-time, then you should use object type for boxing values and then when you need to work with a value in a typed manner, check for its type and unbox it to desired type and use it.

So you can use one of these data structures based on your requirements:

  • Dictionary<CommandType, object> ← The key should be unique.
  • List<KeyValuePair<CommandType, object>> ← There key of pair doesn't need to be unique.

Note: You probably can imagine solutions like creating a common base class BaseType and derive two different ListContainer and EnumContainer from the BaseType and creating ListContainer and EnumContainer at runtime and store in a Dictionary<CommandType, BaseType>. Such structures probably just can help you to limit the storage to desired type rather than using object.

like image 164
Reza Aghaei Avatar answered Nov 15 '22 06:11

Reza Aghaei