Link to home
Start Free TrialLog in
Avatar of patd1
patd1Flag for United States of America

asked on

Declaring and adding rows to a dictionary dynamically using c#

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.
ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patd1

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patd1

ASKER

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.