1

Because the original post (Create List with name from variable) was so old, I didn't want to approach this as an answer.

But, I wanted to add this use of the above solution because it was non-obvious to me. And, it may help some of my fellow noobs... Also, I ran into some issues I don't know how to address.

I needed a way to create a list using a variable name, in this case "mstrClock", for timing diagrams.

I was not able to get .NET to accept a two-column list, though, so I ended up with two dictionaries.

Is there a way to structure this so that I can use a single dictionary for both columns?

dictD.Add("mstrClock", new List<double>());
dictL.Add("mstrClock", new List<string>());

Then as I develop the timing diagram, I add to the lists as follows:

dictD["mstrClock"].Add(x);  // This value will normally be the time value.
dictL["mstrClock"].Add("L");    // This value will be the "L", "F" or "H" logic level

Then to get at the data I did this:

for (int n = 0; n < dictD["mstrClock"].Count; n++)
{
    listBox1.Items.Add(dictL["mstrClock"][n] + "\t" + dictD["mstrClock"][n].ToString());
}
0

3 Answers 3

0

Why not just store what you want to display, in the dictionary?

dict.Add("mstrClock", new List<string>());
dict["mstrClock"].Add($"L\t{x}"); 

for (int n = 0; n < dict["mstrClock"].Count; n++)
{
    listBox1.Items.Add(dict["mstrClock"][n]);
}

On another point, do you even need a dictionary? What is the point of having a dictionary with one key? If you only need a List<string>, then only create that.

var items = new List<string>());
items.Add($"L\t{x}"); 

foreach (var item in items)
{
    listBox1.Items.Add(item);
}
Sign up to request clarification or add additional context in comments.

1 Comment

[Why not just store what you want to display, in the dictionary?] Because I will need to add additional lists (Components) to the dictionary. For instance. mstrClock will be used to drive a 7490 four bit counter, which will then drive a 4-16 decoder.
0

You can use Tuples in modern C# to create your two-column list as follows:

var list = new List<(double time, string logicLevel)>();
list.Add((1, "L"));
list.Add((2, "F"));

foreach (var element in list)
{
    listBox1.Items.Add($"{element.time} \t {element.logicLevel}");
}

If using a dictionary is a must, you can change the above code to something like:

var dict = new Dictionary<string, List<(double time, string logicLevel)>>();

dict["mstrClock"] = new List<(double time, string logicLevel)>();
dict["mstrClock"].Add((1, "L"));
dict["mstrClock"].Add((2, "F"));

var list = dict["mstrClock"];
foreach (var element in list)
{
    listBox1.Items.Add($"{element.time} \t {element.logicLevel}");
}

Comments

0

One approach to creating a 2-column list would be to create a list of key/value pairs:

var list = new List<KeyValuePair<double, string>();
list.Add(new KeyValuePair<double, string>(1, "L");

foreach (KeyValuePair<double, string> element in list)
{
    listBox1.Items.Add($"{element.key} \t {element.value}");
}

Comments

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.