Results 1 to 8 of 8

Thread: c# help

  1. #1
    Josh's Avatar
    Joined
    Dec 2011
    Posts
    415
    Userbars
    2
    Thanks
    25
    Thanked
    378/143
    DL/UL
    82/6
    Mentioned
    120 times
    Time Online
    17d 9h 48m
    Avg. Time Online
    5m

    c# help

    @(you need an account to see links)

    (you need an account to see links)

    I dont get why that doesnt work.

    Its for a kad feeder. Never really worked in c# before. lsttemp is a list containing both the ids and item names. And lstignore is the items to ignore.

    I believe the problem is when it deletes the last thing in the list.
    Code:
    InvalidArgument=Value of '8' is not valid for 'index'.
    ^Thats the error I get.

    And this line is highlighted:
    Code:
    if (lsttemp.Items[i].ToString().Contains(removelistitem))
    Last edited by Josh; 12-11-2012 at 01:27 AM.

  2. #2
    Saiyan Race
    j03's Avatar
    Joined
    Dec 2011
    Posts
    13,720
    Userbars
    166
    Thanks
    5,906
    Thanked
    33,077/6,608
    DL/UL
    23/36
    Mentioned
    3,867 times
    Time Online
    563d 5h 6m
    Avg. Time Online
    3h 13m
    InvalidArgument=Value of '8' is not valid for 'index'.


    There is no 9th item in your list
    (you need an account to see links)
    (you need an account to see links)(you need an account to see links)

    ------------------------
    [02/24/2013] Stealth CORE is made into the first standalone Neopets auto-player.
    ------------------------


  3. #3
    Josh's Avatar
    Joined
    Dec 2011
    Posts
    415
    Userbars
    2
    Thanks
    25
    Thanked
    378/143
    DL/UL
    82/6
    Mentioned
    120 times
    Time Online
    17d 9h 48m
    Avg. Time Online
    5m
    Quote Originally Posted by Infamous Joe View Post
    InvalidArgument=Value of '8' is not valid for 'index'.


    There is no 9th item in your list
    Any clue how to fix it? I know the problem, but Im not too sure about how to fix it. Right now its running with 9 items in the list to begin with. But every time it does it 1 is removed (Right now its only unbuyables showing up for kads)

  4. #4

    Joined
    Nov 2012
    Posts
    26
    Userbars
    1
    Thanks
    0
    Thanked
    18/11
    DL/UL
    6/0
    Mentioned
    7 times
    Time Online
    19h 35m
    Avg. Time Online
    N/A
    The root of the problem is that your inner loop and outer loop should be reversed. Since you can possibly change the list items from the outer loop while you are still running the inner loop, removing the last item from the list will cause problems.

    Here are two different solutions, the first one is simpler, the second reverses your loops so the code should work without error.

    This first example takes advantage of ListBox.FindString which does an efficient search of the list and uses only a single for loop of the ignore list.

    Code:
    for (int b = lstignore.Items.Count - 1; b >= 0; --b) {
        string removelistitem = lstignore.Items[b].ToString();
        int index = lsttemp.FindString(removelistitem);
    
        if (index >= 0) {
            lsttemp.Items.RemoveAt(index);
        }
    }
    The second example is similar to what you had but is fixed by swapping the two loops around so its safe to change the first list in the second loop.

    Code:
    for (int b = lstignore.Items.Count - 1; b >= 0; --b) {
        string removelistitem = lstignore.Items[b].ToString();
    
        for (int i = lsttemp.Items.Count - 1; i >= 0; --i) {
            if (lsttemp.Items[i].ToString().Contains(removelistitem)) {
                lsttemp.Items.RemoveAt(i);
             }
        }
    }
    Hope that helps.

  5. The Following 2 Users Say Thank You to drew010 For This Useful Post:

    j03 (12-11-2012),Josh (12-11-2012)

  6. #5
    Josh's Avatar
    Joined
    Dec 2011
    Posts
    415
    Userbars
    2
    Thanks
    25
    Thanked
    378/143
    DL/UL
    82/6
    Mentioned
    120 times
    Time Online
    17d 9h 48m
    Avg. Time Online
    5m
    That works perfect. Thanks for the help.

  7. #6
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    You should probably use a custom class (ie Item.cs) for storing Item Names with corresponding IDs.

  8. #7
    Josh's Avatar
    Joined
    Dec 2011
    Posts
    415
    Userbars
    2
    Thanks
    25
    Thanked
    378/143
    DL/UL
    82/6
    Mentioned
    120 times
    Time Online
    17d 9h 48m
    Avg. Time Online
    5m
    I like my way.

  9. #8
    Saiyan Race
    j03's Avatar
    Joined
    Dec 2011
    Posts
    13,720
    Userbars
    166
    Thanks
    5,906
    Thanked
    33,077/6,608
    DL/UL
    23/36
    Mentioned
    3,867 times
    Time Online
    563d 5h 6m
    Avg. Time Online
    3h 13m

    c# help

    (you need an account to see links)
    (you need an account to see links)(you need an account to see links)

    ------------------------
    [02/24/2013] Stealth CORE is made into the first standalone Neopets auto-player.
    ------------------------


  10. The Following User Says Thank You to j03 For This Useful Post:

    Zachafer (12-27-2012)

Posting Permissions

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