Click to See Complete Forum and Search --> : sortedlist question
Crash893
Aug 7th, 2007, 08:52 PM
Hi all
I'm totaling up a list of items in a Binding source and I am just having a small problem with syntax for incrementing the value of a key if it already exists
any help is greatly appreciated line 16 below
SortedList mySortedList = new SortedList();
foreach (DataRowView view in myBindingSource)
{
if ((string)view["group_name"] == "USAT-Desktop")
{
view["group_name"] = "TEST TEST";
}
if (mySortedList.Contains(view["category_name"])== false)
{
mySortedList.Add(view["category_name"], 1);
}
else
{
//how do I ++ the value of the key if it already exists?
//mysortedlist[view["category_name"]].value +=1?
}
penagate
Aug 8th, 2007, 02:31 PM
++mysortedlist[view["category_name"]].value;
surely?
Crash893
Aug 8th, 2007, 08:42 PM
i got
mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];
Crash893
Aug 8th, 2007, 08:45 PM
++mysortedlist[view["category_name"]].value;
surely?
there is not .value option for this did you get this to work or are you just sudo coding
Either one is fine i appreciate the help i just want to know if im missing something or not.
jmcilhinney
Aug 8th, 2007, 09:57 PM
This is obviously C# 2005 if you are using a BindingSource, but you should still be specifying when you post. After more than 550 posts you should be able to do that without asking. It is one click of a radio button.
As it's C# 2005 you should be using a generic collection, not the standard type. That way your values will be typed and no casting is required:SortedList<string, int> list=new SortedList<string,int>();
string name;
foreach (DataRowView row in this.bindingSource1)
{
name = (string)row["Name"];
if (list.ContainsKey(name))
{
list[name]++;
}
else
{
list.Add(name, 1);
}
}With a standard SortedList it would have to look like this:SortedList list = new SortedList();
string name;
foreach (DataRowView row in this.bindingSource1)
{
name = (string)row["Name"];
if (list.ContainsKey(name))
{
list[name] = (int)list[name] + 1;
}
else
{
list.Add(name, 1);
}
}
Crash893
Aug 8th, 2007, 11:00 PM
This is obviously C# 2005 if you are using a BindingSource, but you should still be specifying when you post. After more than 550 posts you should be able to do that without asking. It is one click of a radio button.
As it's C# 2005 you should be using a generic collection, not the standard type. That way your values will be typed and no casting is required:
thanks
I never thought about it before I will make sure to note that in the future.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.