Results 1 to 2 of 2

Thread: Multi-dimensional arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    2

    Multi-dimensional arrays

    Hello. I doing this VB project involving an airplane seat reservation system. There are 4 columns of seats and 7 seats in each column. When the user clicks the reserve button the background of the seat label will change from green to red and a message box pops up. When the user hits the button one seat should be reserved. The user can then hit the button again to reserve another seat in the right next to it in the same row. The problem I am having with this is that when I hit the button to reserve a seat the entire row of seats turns red. Any thoughts on how I would make just 1 seat in the row change???

    Here is what I have for code.

    Public Class Form1
    Dim seat_controls(5, 3) As Label
    Dim seats(5, 3) As Integer
    Dim seat_control(8, 5) As Label
    Dim seat(8, 5) As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    seat_controls(0, 0) = lblSeatA1
    seat_controls(1, 0) = lblSeatA2
    seat_controls(2, 0) = lblSeatA3
    seat_controls(3, 0) = lblSeatA4
    seat_controls(4, 0) = lblSeatA5
    seat_controls(5, 0) = lblSeatA6
    seat_controls(0, 1) = lblSeatB1
    seat_controls(1, 1) = lblSeatB2
    seat_controls(2, 1) = lblSeatB3
    seat_controls(3, 1) = lblSeatB4
    seat_controls(4, 1) = lblSeatB5
    seat_controls(5, 1) = lblSeatB6
    seat_controls(0, 2) = lblSeatC1
    seat_controls(1, 2) = lblSeatC2
    seat_controls(2, 2) = lblSeatC3
    seat_controls(3, 2) = lblSeatC4
    seat_controls(4, 2) = lblSeatC5
    seat_controls(5, 2) = lblSeatC6
    seat_controls(0, 3) = lblSeatD1
    seat_controls(1, 3) = lblSeatD2
    seat_controls(2, 3) = lblSeatD3
    seat_controls(3, 3) = lblSeatD4
    seat_controls(4, 3) = lblSeatD5
    seat_controls(5, 3) = lblSeatD6
    seat_control(0, 0) = lblSeatA7
    seat_control(1, 0) = lblSeatA8
    seat_control(2, 0) = lblSeatA9
    seat_control(3, 0) = lblSeatA10
    seat_control(4, 0) = lblSeatA11
    seat_control(5, 0) = lblSeatA12
    seat_control(6, 0) = lblSeaA13
    seat_control(7, 0) = lblSeatA14
    seat_control(8, 0) = lblSeatA15
    seat_control(0, 1) = lblSeatB7
    seat_control(1, 1) = lblSeatB8
    seat_control(2, 1) = lblSeatB9
    seat_control(3, 1) = lblSeatB10
    seat_control(4, 1) = lblSeatB11
    seat_control(5, 1) = lblSeatB12
    seat_control(6, 1) = lblSeatB13
    seat_control(7, 1) = lblSeatB14
    seat_control(8, 1) = lblSeatB15
    seat_control(0, 2) = lblSeatC7
    seat_control(1, 2) = lblSeatC8
    seat_control(2, 2) = lblSeatC9
    seat_control(3, 2) = lblSeatC10
    seat_control(4, 2) = lblSeatC11
    seat_control(5, 2) = lblSeatC12
    seat_control(6, 2) = lblSeatC13
    seat_control(7, 2) = lblSeatC14
    seat_control(8, 2) = lblSeatC15
    seat_control(0, 3) = lblSeatD7
    seat_control(1, 3) = lblSeatD8
    seat_control(2, 3) = lblSeatD9
    seat_control(3, 3) = lblSeatD10
    seat_control(4, 3) = lblSeatD11
    seat_control(5, 3) = lblSeatD12
    seat_control(6, 3) = lblSeatD13
    seat_control(7, 3) = lblSeatD14
    seat_control(8, 3) = lblSeatD15
    seat_control(0, 4) = LabelE7
    seat_control(1, 4) = LabelE8
    seat_control(2, 4) = LabelE9
    seat_control(3, 4) = LabelE10
    seat_control(4, 4) = LabelE11
    seat_control(5, 4) = LabelE12
    seat_control(6, 4) = LabelE13
    seat_control(7, 4) = LabelE14
    seat_control(8, 4) = LabelE15
    seat_control(0, 5) = LabelF7
    seat_control(1, 5) = LabelF8
    seat_control(2, 5) = LabelF9
    seat_control(3, 5) = LabelF10
    seat_control(4, 5) = LabelF11
    seat_control(5, 5) = LabelF12
    seat_control(6, 5) = LabelF13
    seat_control(7, 5) = LabelF14
    seat_control(8, 5) = LabelF15
    End Sub


    Private Sub btnReserve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReserve.Click
    If RadioButton1.Checked = True Then
    If seat_controls(5, 3).BackColor = Color.Red Then
    MsgBox("There are no more first class seats available.")
    Else

    For j As Integer = 0 To 3
    For i As Integer = 0 To 5
    If seats(i, j) = 0 Then
    seats(i, j) = 1
    seat_controls(i, j).BackColor = Color.Red
    MsgBox("Your seat has been reserved.")
    Exit For
    seats(i, j) = 1
    Exit For
    End If
    Next
    Next
    End If
    End If
    End Sub
    End CLass

    Any thoughts on how to fix the code to make it only change one seat at a time?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Multi-dimensional arrays

    vb Code:
    1. For j As Integer = 0 To 3
    2.     For i As Integer = 0 To 5
    3.         If seats(i, j) = 0 Then
    4.             seats(i, j) = 1
    5.             seat_controls(i, j).BackColor = Color.Red
    6.             MsgBox("Your seat has been reserved.")
    7.             return
    8.         End If
    9.     Next
    10. Next

Tags for this Thread

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