Results 1 to 6 of 6

Thread: sortedlist question

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    sortedlist question

    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


    C# Code:
    1. SortedList mySortedList = new SortedList();
    2.             foreach (DataRowView view in myBindingSource)
    3.             {
    4.                 if ((string)view["group_name"] == "USAT-Desktop")
    5.                 {
    6.                     view["group_name"] = "TEST TEST";
    7.                 }
    8.                
    9.                
    10.                if (mySortedList.Contains(view["category_name"])== false)
    11.                {
    12.                    mySortedList.Add(view["category_name"], 1);
    13.                }
    14.                else
    15.                {
    16.                //how do I ++ the value of the key if it already exists?
    17.                //mysortedlist[view["category_name"]].value +=1?
    18.                }

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: sortedlist question

    ++mysortedlist[view["category_name"]].value;

    surely?

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: sortedlist question

    i got

    mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: sortedlist question

    Quote Originally Posted by penagate
    ++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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: sortedlist question

    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:
    Code:
    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:
    Code:
    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);
    	}
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: sortedlist question

    Quote Originally Posted by jmcilhinney
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width