Results 1 to 5 of 5

Thread: [RESOLVED] Copying array contents to listboxes

  1. #1

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    57

    Resolved [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?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    57

    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.

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Copying array contents to listboxes

    Quote 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.

  5. #5

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    57

    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
  •  



Click Here to Expand Forum to Full Width