patd1
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:
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.
Thanks for your help in advance.
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>();
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); }
Thanks for your help in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Open in new window
Thanks.