Results 1 to 5 of 5

Thread: Simple Array?

  1. #1
    Join Date
    Dec 2004
    Posts
    2

    Unanswered: Simple Array?

    Hello,

    I am new to VB and this forum. I have been learning VB6.0 for about 2 weeks.

    It seems that I have made arrays work in other situations, but this one is killing me.

    What is wrong with this?

    Private Sub button7_Click()

    ClientRoomPicks.Clear

    Dim fnum2() As Integer

    fnum2 = 1

    Do
    If RoomSelectionList.Selected(fnum2) = True Then ClientRoomPicks.AddItem roomname(fnum2 + 1)
    fnum2 = fnum2 + 1
    Loop

    End Sub

    Everytime I click the button I get the error message Compile Error: "Can't assign to array"

    Thanks for any help.

  2. #2
    Join Date
    May 2004
    Posts
    97
    fnum2 is an integer but you dimmed it as an array of integers, and roomname you did not declare at all, but IS an array. Unless you dimmed is at the form level.... Try this:

    Code:
    Private Sub button7_Click()
    
    ClientRoomPicks.Clear
    
    Dim fnum2 As Integer
    Dim roomname() as integer ' or string, whatever
    
    fnum2 = 1
    
    Do
    If RoomSelectionList.Selected(fnum2) = True Then ClientRoomPicks.AddItem roomname(fnum2 + 1)
    fnum2 = fnum2 + 1
    Loop
    
    End Sub

  3. #3
    Join Date
    Nov 2004
    Posts
    108

    Try For/Next

    Code:
    Private Sub cmdClientRoomPick_Click()
         Dim listIndex As Integer
         ClientRoomPicks.Clear
         For listIndex = 0 to RoomSelectionList.ListCount -1     'this makes your code more dynamic
              If RoomSelectionList.Selected(listIndex) = True Then 
                   ClientRoomPicks.AddItem RoomSelectionList.List(listIndex)
    '***           Exit For  'remove comment if only one item can be selected
              End If
         Next
    End Sub
    When performing a loop through all the members of an array try always using ubound, lbound, Count, listCount, etc. This way your lists, arrays etc can grow without the need of altering the code over and over
    to err is human ; to really mess things up requires a computer

  4. #4
    Join Date
    Nov 2004
    Posts
    108
    I edited a bit MTaylor's code ...

    Code:
    Private Sub button7_Click()
         Dim fnum2 As Integer
         Dim roomname() as string ' or integer, whatever
    
         ClientRoomPicks.Clear
    
         'roomname() is empty;a redim is needed and also values
         'Redim roomName(5) as string
         'roomName(1)="kitchen"  'asuming base is 1
         'roomName(2)="bathroom"
         '...
    
         fnum2 = 1
    
         Do While fnum2<RoomSelectionList.listCount 'dynamic but not infinitive
              If RoomSelectionList.Selected(fnum2) = True Then _
                   ClientRoomPicks.AddItem roomname(fnum2 + 1) 
              fnum2 = fnum2 + 1
         Loop
    
    End Sub
    to err is human ; to really mess things up requires a computer

  5. #5
    Join Date
    Dec 2004
    Posts
    2
    Thank you very much!

    I really appreciate this site and the help it offers. I know I will have some more elementary questions in the very near future. Hopefully soon I will be able to answer those questions for other beginners.

    Again, thanks a bunch.

Posting Permissions

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