We help IT Professionals succeed at work.

Declaring and adding rows to a dictionary dynamically using c#

patd1
patd1 asked
on
Medium Priority
282 Views
Last Modified: 2012-05-11
I have certain case types in a List.  I have to declare a Dictionary for each case type in the list and then add rows to the dictionary. How can I do it dynamically?

example: the list has the following case types: 'Blood, Summary, Bone, Tissue"
How do I do the following so that it dynamically creates a Dictionary for each case type:
Dictionary<string, DateTime> BloodReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> SummaryReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> BoneReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> TissueReport = new Dictionary<string, DateTime>();

Open in new window


Then the case number list is like this:
Bone1234, Blood1243, Bone2345, Summary1234

And I want to add those cases to the respective Dictionary. Normal code will be like this, but how can I make it dynamic.
if (casenum.StartsWith("Blood", true, ci))
                    { BloodReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Bone", true, ci))
                    { BoneReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Tissue", true, ci))
                    { TissueReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Summary", true, ci))
                    { SummaryReports.Add(casenum, CompletedDate); }

Open in new window


Thanks for your help in advance.
Comment
Watch Question

Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I understand this is adding dictionary as an item in the caseTypeDictionary. Now how do I add items to these dictionaries from the case number list.
if (casenum.StartsWith("Blood", true, ci))
                    { BloodReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Bone", true, ci))
                    { BoneReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Tissue", true, ci))
                    { TissueReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Summary", true, ci))
                    { SummaryReports.Add(casenum, CompletedDate); }

Open in new window


Thanks.
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
But it would be better if your case record actually had the case type in it (rather than "Blood123" it had "Blood"), because you could replace all of the if/then/elses with one add:

I may have multiple Blood case numbers in my case number list (Blood123, Blood 234 etc)

The code that you provided above makes sense to me. I will definitely try this out ASAP.

Thanks for you help.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.