By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Dicitionary Geneic Class

P: 18
Hi All,
Can anyone help me out regarding Dicitonary Generic Class using c#.

Like I have 3 btns(add, edit and delete), text box , LisBox, Listview. If i add an item from Text it should be added to lIstbox and i have items in listview. They should be assigned to listbox selected item. these can be done through dicitonary classes. If i add new one Listbox item and new listview items are to be added to that selected item.
Jan 24 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 190
The ListBox displays a ListBox.ObjectCollection, which is simply a collection of objects.
The ListView contains a ListViewItemCollection which is a collection of ListViewItems.
A Dictionary has a collection of KeyValuePairs.

If you want the ListBox to somehow reflect the combination of the TextBox value and the ListView item, you might consider making the each ListBox item a KeyValuePair<string, ListViewItem>.
Jan 25 '09 #2

P: 18
I have Listbox(lstgroups) and listview(lstItems). say items in lisbox as Groups and in listview as Items.
Each Group should have list of items, when i add an new item to lstitems. it should assign to selected group.
By using dictionary classes
Here List<object> should return lstitems

Dictionary<string, List<object>> d= new Dictionary(string, List<object>>();

d.add(lstgroups.items, new List<object>);

//error cumes here, how to assign here List<>
Jan 27 '09 #3

Expert 100+
P: 229
Here is an example of using Dictionary<>: System.Collections.Generic.Dictionary.

Try to explain what is the goal you are trying to accomplish, maybe you don't need a dictionary?

If you want to assing a list of items to a "group", the easiest way would be to create that list first, and then add it to the dictionary:

Expand|Select|Wrap|Line Numbers
  1. // create a list
  2. List<Object> someItems = new List<Object>;
  4. // add some items
  5. someItems.AddRange( /* add the items */);
  7. // add these items with a specified key ("group name")
  8. d.Add(groupName, someItems);
Note also:
1. you have to do this for each group
2. you cannot add the same groupName twice (it will raise an exception)
Jan 27 '09 #4

Expert 100+
P: 190
Small syntax note:
Expand|Select|Wrap|Line Numbers
  1. List<Object> someItems = new List<Object>();
Jan 27 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.