Adding to a Combo Box

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
OK, I am sure this is a really basic problem, but for some reason, the answer is eluding me...

Here is the background:
-I have a list of ranks, ex. Basic, Basic, Basic, Leader, Basic, Basic, Basic, Leader, General, Basic. They are stored in an array such as _arrMemberList(1).Rank.

-I want to add one of each to a Combo Box. Here is the code I have written


1. Private Sub DisplayGuildRanks(ByVal _arrMemList() As GuildMember)

2. Dim exists As Boolean = False

3. For _intMember As Int32 = 1 To _arrMemList.GetUpperBound(0)

4. For _intAddedRank As Int32 = 1 To cmbRankList.Items.Count

5. If cmbRankList.Items(_intAddedRank) = _arrMemList(_intMember).Rank Then

6. exists = True

7. End If

8. Next

9. If exists = False Then

10. cmbRankList.Items.Add(_arrMemList(_intMember).Rank)

11. End If

12. Next

13. End Sub

The cmbRankList has no items in it previous to this, and it keeps crashing on line 5. With the error:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid values.
I was getting frusturated so I decided to call it a night.
 
Your nested For loop is set up to run from 1 to List.Items.Count, but the list is zero-based, so when the loop runs for the count(th) time, there is no element to examine. Try changing the loop counter to run from 0 to (count-1).
 
do you ever have moments you want to smack yourself in the face?

When the answer was so obvious that when you finaly figure it out you just want to scream?

This is one of those cases. The I had tried changing to counter before, but I had made it only 0 to .count, or 1 to .count-1 but that didnt help, When I used the 0 to count-1 it only was adding 2 names...

So I went into debug mode and selected most of the variables in the sub and added them to watch. After only a few minutes I noticed the problem. The boolean was not being set to false again after it was changed to true. This was causeing it to do everything right in the inner for loop, but when it went to add the rank, it saw the boolean was still true, so it just skipped on by... With a quick fix everything is working now.

I think your help and a good nights rest helped me figure out my problem. Thanks a ton.
 
sswing11 said:
Your nested For loop is set up to run from 1 to List.Items.Count, but the list is zero-based, so when the loop runs for the count(th) time, there is no element to examine. Try changing the loop counter to run from 0 to (count-1).
you've got a good eye, even i didnt see that :)
 
Back
Top