Stuck on crazy Dictionary/List problem...


V

vbMark

I'm stuck on this crazy problem. I need to get this list into this type
of dictionary object: Dictionary<string, List>

I have this collection passed to me named "items"

-Animal
Dog
Cat
Cow
-Food
Apple
Nut
Carrot
-Vehicle
Car
Boat
Train

The string part of the dictionary object needs to be the word with the
dash and the items below it needs to go into its corresponding list
object.

I need to do this for dual drop-down boxes so that when a dashed item is
selected in one drop-down I can populate the other drop-down box with its
corresponding list.

Here is a rough structure of how I started but of course it does not
work:

ContainersCollection items = new ContainersCollection();
Dictionary<string, List> myDict = new Dictionary<string, List>();
List<string> myList = new List<string>();
String ContainerName;
foreach (string item in items)
{
if (item.StartsWith("-"))
ContainerName = item;
else
myList.Add(item);

//Do something here???

myDict.Add(ContainerName, myList);
}

Any ideas?

Thanks!!!
Mark
 
Ad

Advertisements

F

forever.zet

I'm stuck on this crazy problem. šI need to get this list into this type
of dictionary object: Dictionary<string, List>

I have this collection passed to me named "items"

-Animal
Dog
Cat
Cow
-Food
Apple
Nut
Carrot
-Vehicle
Car
Boat
Train

The string part of the dictionary object needs to be the word with the
dash and the items below it needs to go into its corresponding list
object.

I need to do this for dual drop-down boxes so that when a dashed item is
selected in one drop-down I can populate the other drop-down box with its
corresponding list.

Here is a rough structure of how I started but of course it does not
work:

ContainersCollection items = new ContainersCollection();
Dictionary<string, List> myDict = new Dictionary<string, List>();
List<string> myList = new List<string>();
String ContainerName;
foreach (string item in items)
{
š šif (item.StartsWith("-"))
š š š ContainerName = item;
š šelse
š š š myList.Add(item);

š //Do something here???

š šmyDict.Add(ContainerName, myList);

}

Any ideas?

Thanks!!!
Mark

I think there's conflict between generic and not generic List
implementations.
It seems you need either to declare myDict as Dictionary<string,
List<string>> or
declare myList as List.

Thanks,
Sergey Zyuzin
 
V

vbMark

I think there's conflict between generic and not generic List
implementations.
It seems you need either to declare myDict as Dictionary<string,
List<string>> or
declare myList as List.

Thanks,
Sergey Zyuzin

Hi Sergy,

I'm not getting any errors, that's not the problem. The problem is that
I can not figure out the logic to get the items with-out dashes separated
from the items with dashes and then put into the Dictionary as a <string,
List> pair.

Thanks,
Mark
 
P

Peter Duniho

[...]
Here is a rough structure of how I started but of course it does not
work:

ContainersCollection items = new ContainersCollection();
Dictionary<string, List> myDict = new Dictionary<string, List>();
List<string> myList = new List<string>();
String ContainerName;
foreach (string item in items)
{
if (item.StartsWith("-"))
ContainerName = item;
else
myList.Add(item);

//Do something here???
myDict.Add(ContainerName, myList);
}

Well, your main problem is that you're adding the list to the dictionary
for each element in the original list. The second time you try that fora
given "ContainerName", you'll get an exception because there's already an
element in the dictionary with that key.

I also don't what's up with "Dictionary<string, List>". I didn't even
know you could use that syntax. But you say it compiles fine, so I'll
take your word for that.

Anyway, logically you want to initialize a new list each time you hit a
new container name, and you want to add new non-container names to that
list. Seems to me it'd look something like this:

Dictionary<string, List<string>> myDict = new Dictionary<string,
List<string>>();
List<string> myList = null;

foreach (string item in items)
{
if (item.StartsWith("-"))
{
myList = new List<string>();
myDict.Add(item.Substring(1), myList);
}
else
{
if (myList != null)
{
myList.Add(item);
}
else
{
// bad data!
}
}
}

Pete
 
L

Linda Liu[MSFT]

Hi Mark,
The problem is that I can not figure out the logic to get the items
with-out dashes separated from the items with dashes and then put into the
Dictionary as a <string, List> pair.

The following is a sample to get what you want:

List<string> items = new List<string>();
items.Add("-Animal");
items.Add("Dog");
items.Add("Cat");
items.Add("Cow");
items.Add("-Food");
items.Add("Apple");
items.Add("Nut");
items.Add("Carrot");
items.Add("-Vehicle");
items.Add("Car");
items.Add("Boat");
items.Add("Train");

Dictionary<string, IList> myDict = new Dictionary<string,
IList>();
List<string> myList=null;
string containerName="";
foreach (string item in items)
{
if (item.StartsWith("-"))
{
if (containerName != "")
{
myDict.Add(containerName, myList);
}
containerName = item;
myList = new List<string>();
}
else
{
myList.Add(item);
}
}
if (containerName != "")
{
myDict.Add(containerName, myList);
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

vbMark

[...]
Here is a rough structure of how I started but of course it does not
work:

ContainersCollection items = new ContainersCollection();
Dictionary<string, List> myDict = new Dictionary<string, List>();
List<string> myList = new List<string>();
String ContainerName;
foreach (string item in items)
{
if (item.StartsWith("-"))
ContainerName = item;
else
myList.Add(item);

//Do something here???
myDict.Add(ContainerName, myList);
}

Well, your main problem is that you're adding the list to the
dictionary for each element in the original list. The second time
you try that for a given "ContainerName", you'll get an exception
because there's already an element in the dictionary with that key.

I also don't what's up with "Dictionary<string, List>". I didn't even
know you could use that syntax. But you say it compiles fine, so
I'll take your word for that.

Anyway, logically you want to initialize a new list each time you hit
a new container name, and you want to add new non-container names to
that list. Seems to me it'd look something like this:

Dictionary<string, List<string>> myDict = new Dictionary<string,
List<string>>();
List<string> myList = null;

foreach (string item in items)
{
if (item.StartsWith("-"))
{
myList = new List<string>();
myDict.Add(item.Substring(1), myList);
}
else
{
if (myList != null)
{
myList.Add(item);
}
else
{
// bad data!
}
}
}

Pete

I got it working late last night. First of all, thank you Pete and Linda
for your help. Here is what I came up with....

if (item.StartsWith("-"))
{
ContainerName = item;
myDict.Add(ContainerName, new List<string>());
}
else
{
myDict[ContainerName].Add(item);
}

First it adds a new dictionary item with an empty list then add subsequent
items to that list until the next main item.

Cool, huh?

Thanks again to everyone!
Mark
 
Ad

Advertisements

P

Peter Duniho

[...]
if (item.StartsWith("-"))
{
ContainerName = item;
myDict.Add(ContainerName, new List<string>());
}
else
{
myDict[ContainerName].Add(item);
}

First it adds a new dictionary item with an empty list then add
subsequent
items to that list until the next main item.

Why are you doing it that way? My method should have worked. The version
you posted adds extra overhead, in the form of the dictionary indexer
being called for each non-container item. My version just retains the
appropriate list reference and adds the item directly, without the
overhead.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top