Reputation: 8461
My application requires I print N amount of times the Value X.
So, I can do this:
Dictionary<int, string> toPrint = new Dictionary<int, string>();
toPrint.Add(2, "Hello World");
...and later on I can use this information to print 2 pages, both with the text value "Hello World".
The issue I have, is the Dictionary really wants the first value to be the Key:
Dictionary<TKey, TValue>
Therefore, if I want to add 2 pages with the text value "Hello World" and then another 2 with "Goodbye World" I have an issue - both of them have a TKey value of 2 and this causes a runtime error ("An item with the same key has already been added").
The logic which will causes an error:
Dictionary<int, string> toPrint = new Dictionary<int, string>();
toPrint.Add(2, "Hello World");
toPrint.Add(2, "Goodbye World");
I still need this concept/logic to work, but I obviously can't use the Dictionary type due to the Key.
Does any one have any ideas of a work around?
Upvotes: 4
Views: 14003
Reputation: 216303
Using a List<T> will be enough in this case
class PrintJob
{
public int printRepeat {get; set;}
public string printText {get; set;}
// If required, you could add more fields
}
List<PrintJob> printJobs = new List<PrintJob>()
{
new PrintJob{printRepeat = 2, printText = "Hello World"},
new PrintJob{printRepeat = 2, printText = "Goodbye World"}
}
foreach(PrintJob p in printJobs)
// do the work
Upvotes: 13
Reputation: 23123
I think a Tuple would be perfect for this job.
List<Tuple<int, string>> toPrint = new List<Tuple<int, string>>();
toPrint.Add(new Tuple<int, string>(2, "Hello World");
toPrint.Add(new Tuple<int, string>(2, "Goodbye World");
And... you could easily wrap this into a self contained class.
public class PrintJobs
{
// ctor logic here
private readonly List<Tuple<int, string>> _printJobs = new List<Tuple<int, string>>();
public void AddJob(string value, int count = 1) // default to 1 copy
{
this._printJobs.Add(new Tuple<int, string>(count, value));
}
public void PrintAllJobs()
{
foreach(var j in this._printJobs)
{
// print job
}
}
}
}
Upvotes: 15
Reputation: 528
You can use dictionaries, but the key should be the string, not the int; that's what is unique after all!
That said, you're not doing look-ups so a dictionary is inappropriate. Steve's answer is likely best in this situation though.
Upvotes: 1
Reputation: 13600
Well, I believe you have couple of options here...
1.) in your scenario it seems the string itself is the key, so you can just reverse the order of your parameters
new Dictionary<string, int> ()
2.) use Tuple or even a custom class/struct if it makes sense in your situation. Usage of tuple Chris has already shown you, so I'll show you the "class solution" I have in mind.
public class MyClass
{
public string MyTextToPrint { get;set; }
public string NumberOfPrints { get;set; }
// any other variables you may need
}
and then just create a List of these classes, works pretty much the same as Tuple, it's just a more standardized way of doing this, because maybe you will need same the functionality on other places as well or maybe want to manipulate the data further.
Upvotes: 0