?
Solved

Declaring and adding rows to a dictionary dynamically using c#

Posted on 2011-04-29
4
Medium Priority
?
263 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.
0
Comment
Question by:patd1
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 2000 total points
ID: 35492739
Something like this, assuming you have a list of case types already (as you said).

var caseTypeDictionary = new Dictionary<string, Dictionary<string, DateTime>>();

foreach(var caseType in caseTypeList) {
   caseTypeDictionary.Add(caseType.Name, new Dictionary<string,DateTime>());
}
0
 

Author Comment

by:patd1
ID: 35492855
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.
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 2000 total points
ID: 35492949
if(casenum.StartsWith("Blood", true, ci))
   caseTypeDictionary["Blood"].Add(casenum, CompletedDate);
else if(...)
etc.

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:

   caseTypeDictionary[case.caseType].Add(caes.casenum, case.CompletedDate);

0
 

Author Comment

by:patd1
ID: 35492980
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question