Results 1 to 4 of 4

Thread: using 2 arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Buffalo,NY
    Posts
    6

    using 2 arrays

    Hi everyone. I'm a newbie at vb and have to write a program using 2 arrays,one with student names and the other with student scores.I have to 1st print them out in original order and then sort them from best score to worst. I'm trying to do this using a list box,with a for/next loop and additem. Below is my current code. I know it is wrong and keep getting invalid qualifier errors.Any help would be greatly appreciated.
    Option Explicit
    Dim namearray(6) As String
    Dim lstnamearray As Integer
    Dim i As Integer
    Private Sub Command1_Click()
    Dim i As Integer
    i = lstnamearray
    lstnamearray.Clear
    lstnamearray(0).AddItem = "Dan Pickett 92"
    lstnamearray(1).AddItem = "Betty Knapp 87"
    lstnamearray(2).AddItem = "Jackie Denhoff 82"
    lstnamearray(3).AddItem = "Mike Diebold 78"
    lstnamearray(4).AddItem = "Rob Wardell 73"
    lstnamearray(5).AddItem = "Mike Garcia 70"
    lstnamearray(6).AddItem = "Paul Martinez 66"
    Cls
    For i = 0 To UBound(namearray)
    Print namearray(i)
    Next i
    End Sub

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Put this in a form and test it....look at the comments I added.

    Option Explicit
    Dim namearray(6) As String
    'Dim lstnamearray As Integer ' same name as listbox....no good
    ' this explains the "invalid qualifier errors"
    Dim i As Integer
    Private Sub Command1_Click()
    Dim i As Integer
    'i = lstnamearray ' see above
    lstnamearray.Clear

    ' with listboxes you don't specify an "array" element until to
    ' want to access a particular value.
    lstnamearray.AddItem "Dan Pickett 92"
    lstnamearray.AddItem "Betty Knapp 87"
    lstnamearray.AddItem "Jackie Denhoff 82"
    lstnamearray.AddItem "Mike Diebold 78"
    lstnamearray.AddItem "Rob Wardell 73"
    lstnamearray.AddItem "Mike Garcia 70"
    lstnamearray.AddItem "Paul Martinez 66"
    Cls

    ' not sure what your doing here....namearray is empty
    For i = 0 To UBound(namearray)
    Print namearray(i)
    Next i
    End Sub

  3. #3
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Just noticed..you have i as Integer declared 2x. At the top and in the commandbutton click event. Delete one of them.

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Pickett
    Here is a quick sample. I do hope that you dont just copy it and rather go thru it to understand what each line does. This is just one way of doing it and is based on assumptions such as the score being a whole number. I wanted to keep it simple tho.
    Regards
    Stuart
    VB Code:
    1. Option Explicit
    2.  
    3. Dim fstrNames() As String 'Dynamic declaration is better than fixed if it can change
    4. Dim fintScores() As Integer 'Dynamic declaration is better than fixed if it can change
    5. 'You can also use a user defined type to store both values
    6. 'in one variable but we will leave that
    7. Dim fintNumberOfStudents As Integer
    8. Dim fintLoopCounter As Integer
    9. Dim fintOrderPosition As Integer
    10.  
    11. Private Sub Form_Load()
    12. 'Lets assume that you are not reading your
    13. 'data from a file
    14.     ReDim fstrNames(4) 'U dont need to do this if u declare as fixed length above
    15.     fstrNames(0) = "John Smith"
    16.     fstrNames(1) = "Jenny Jones"
    17.     fstrNames(2) = "Dora Dent"
    18.     fstrNames(3) = "Roger Ramjet"
    19.     fstrNames(4) = "Kevin Kev"
    20.    
    21.     'Assume whole numbers. Otherwise
    22.     'use Single or Double variables
    23.     ReDim fintScores(4) 'U dont need to do this if u declare as fixed length above
    24.     fintScores(0) = 75
    25.     fintScores(1) = 60
    26.     fintScores(2) = 80
    27.     fintScores(3) = 95
    28.     fintScores(4) = 20
    29.    
    30.     'Use this if file can change in number of entries
    31.     fintNumberOfStudents = UBound(fintScores)
    32.     'Or just use fintNumberOfStudents = 4
    33. End Sub
    34.  
    35. Private Sub Command1_Click()
    36.     'Stage 1 Display in actual order
    37.     List1.Clear
    38.     For fintLoopCounter = 0 To fintNumberOfStudents
    39.         'NB List 1 is set to SORTED = FALSE in properties
    40.         List1.AddItem fstrNames(fintLoopCounter) & vbTab & fintScores(fintLoopCounter)
    41.     Next
    42. End Sub
    43.  
    44. Private Sub Command2_Click()
    45.     'Stage 2 Display in score order from best to worst
    46.     List1.Clear
    47.     List2.Clear 'Maybe make List2 Visible false to hide what is going on
    48.     For fintLoopCounter = 0 To fintNumberOfStudents
    49.         'NB List 2 is set to SORTED = TRUE in properties
    50.         List2.AddItem fintScores(fintLoopCounter) 'Scores are all u are sorting so leave names out
    51.         List2.ItemData(List2.NewIndex) = fintLoopCounter
    52.     Next
    53.  
    54.     'Note counting backwards here
    55.     For fintLoopCounter = fintNumberOfStudents To 0 Step -1
    56.         'NB List 1 is set to SORTED = FALSE in properties
    57.         fintOrderPosition = List2.ItemData(fintLoopCounter)
    58.         List1.AddItem fstrNames(fintOrderPosition) & vbTab & fintScores(fintOrderPosition)
    59.     Next
    60. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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