5 Replies - 2173 Views - Last Post: 10 March 2013 - 06:13 AM Rate Topic: -----

#1 gamers18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 13

C# printing Dictionary<> with List<>

Posted 09 March 2013 - 05:29 PM

Hi, ive been trying to print out a dictionary item with its List but im so confused about it.

Basically I created a dictionary and I managed to add an Item to the dictionary(as a Key) and List of something (as value)
Dictionary<String, List<string>> dic = new Dictionary<String, List<string>>();

public void addItem(Item newItem)
     {
         List<string> info = new List<string>();
         dic.Add(newItem.getItemName(), info);
         info.Add(newItem.getDescription());
         info.Add(newItem.getManufacturer());
     }



Im confused how am I going to print an Item in the dictionary with its description and manufacturer like if im going to add a code
public void listItemInfo(String itemName)
{

}



any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: C# printing Dictionary<> with List<>

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: C# printing Dictionary<> with List<>

Posted 09 March 2013 - 05:31 PM

I don't see why you have a Dictionary of strings to Lists.

Instead you can have one of strings to Item objects which makes it all very trivial.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# printing Dictionary<> with List<>

Posted 09 March 2013 - 05:52 PM

One: Make a class to hold your data, with properties

class Widget
{
   public string Description { get; set; }
   public string Maker { get; set; }
}


Then your dictionary becomes
Dictionary(string, Widget)


Then you make a new Widget *THEN* add it. Not add it then fill out the details.

public void addItem()
{
    Widget w = new Widget()
    w.Description = "Something";
    w.Maker = "Spacely Sprockets"
    dic.Add("12345", w);
}



And I have no idea what you're going on about 'printing' - there is nothing here having a thing to do with printing but you've mentioned it twice and it didn't make sense in either context.

This is an object oriented language - make and use objects.
Was This Post Helpful? 0
  • +
  • -

#4 gamers18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 13

Re: C# printing Dictionary<> with List<>

Posted 09 March 2013 - 06:03 PM

Actually Im using console application for this. Sorry forgot to mention. In the application I have options to add Items (add items to dictionary) as well as its info (dictionary's List). Then I have an option to view a specific item
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: C# printing Dictionary<> with List<>

Posted 09 March 2013 - 06:25 PM

gamers18 Think. You have a collection and you should know that you can use the foreach loop to iterate through each item in said collection.
foreach(item in coll)
{
// do something
}


So if the item in the collection is also another collection, why not iterate through that one also?

Not so hard is it? Take time out to think about the code, you are going to produce.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# printing Dictionary<> with List<>

Posted 10 March 2013 - 06:13 AM

View Postgamers18, on 09 March 2013 - 07:03 PM, said:

Actually Im using console application for this. Sorry forgot to mention. In the application I have options to add Items (add items to dictionary) as well as its info (dictionary's List). Then I have an option to view a specific item


None of the suggestions made are specific to console, WinForms or WPF.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1