|
-
Feb 26th, 2009, 05:16 AM
#1
Thread Starter
Member
[RESOLVED] Copying array contents to listboxes
I am having a hell of a time figuring out where I am going wrong in copying the contents of arrays to listboxes. I am sure it is quite simple but I just cannot figure out where the code is going wrong.
I am using textboxes to input team numbers and team names.
The code for that is as follows (used in a button after the data has been entered in the text boxes:
Teamname, Teamnumber and numteams have been previously declared as public variables (string,integer and integer) in a module as they are used throughout the project.
Numteams has had a value ascribed to it in a previous form
ReDim Teamname(Numteams)
ReDim Teamnumber(Numteams)
Dim thisteam As Integer
thisteam = (Val(TxtNumber.Text) - 1)
Teamname(thisteam) = TxtNames.Text
Teamnumber(thisteam) = Val(TxtNumber.Text)
TxtNumber.Text = ""
TxtNames.Text = ""
TxtNumber.Focus()
Once all of the data has been entered, I am trying to transfer the data to listboxes on another form as follows:
For i = 0 To (Numteams - 1)
Results.Tmname.Items.Add("" &Teamname(i))
Results.TmNumber.Items.Add(Teamnumber(i))
Next
I have had to put in the "" for the Teamname variable as otherwise I was getting an error saying "Value cannot be null". I am also not sure what is causing that.
When I do that, I get 0 in the listboxes for all the teamnumbers except the last one which is right and blank strings for the team names except for the last one which is also right.
Can somone point me in the right direction please?
-
Feb 26th, 2009, 06:17 AM
#2
Re: Copying array contents to listboxes
I think you are using different variables (maybe a typo in variable names).
Put an OPTION EXPLICIT on the top of your forms/classes and that should make it clear.
-
Feb 26th, 2009, 06:49 AM
#3
Thread Starter
Member
Re: Copying array contents to listboxes
Checked for typos; none. If there were, it wouldn't be able to fill in the last element of the array.
-
Feb 26th, 2009, 07:10 AM
#4
Re: Copying array contents to listboxes
 Originally Posted by boutells
I have had to put in the "" for the Teamname variable as otherwise I was getting an error saying "Value cannot be null". I am also not sure what is causing that.
When I do that, I get 0 in the listboxes for all the teamnumbers except the last one which is right and blank strings for the team names except for the last one which is also right.
Can somone point me in the right direction please?
The "" & text issue is just how the control handles data. It's been like this since classic VB.
As far as your other issue, the obvious issue I can see is that you are reinitializing your array each time you with to load it. You need to initialize it once, and once only if you have a concrete number of items that you know of. The other issue is that when you fill in an array, lets say Array(5). If you fill in 0,1, and 5, that leaves 3, and 4 still NULL.
As an alternative to the array, have you thought of using the dictionary object instead? It will only hold the data which it does have. In your case, it would be declared as
Code:
Dim D as new dictionary(Of integer, string)
The team number would be your key in this instance.
-
Feb 26th, 2009, 09:44 AM
#5
Thread Starter
Member
Re: Copying array contents to listboxes
Thanks for that; great spot. Haven't used the dictionary yet but I will give it a look.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|