Two dimension Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meena76
    New Member
    • Nov 2006
    • 9

    Two dimension Arrays

    I posted my problem in ref to my Airline Reservation Program. I am using VB 2005 for this program

    I have to right a program that req a users name for a text box then assigns the user a seat (row(x), seat(x). The seat assignment is based on a two dimensional array seatingchart(9, 3). When the user enters name in the text box a click event must be activated in order to store the passenger in a variable string passenger. I also have a control array btnseatresv(9,3 ) that display a 10 X 4 tabular table of button which signifies the seat location, if a seat is red then the seat is already taken if the seat is green then the seat is open. each button has a click event on it. I am having some problems with the logic behind the code.

    1. When the passenger name is typed in the text box and the addpassenger button is clicked it assigns the variable to the passenger name code below:

    passenger = txtPassName.Tex t

    then an input box is displayed which then ask the user to enter the row pass wants to seat in. (See Below)
    rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
    next another input box is displayed which then ask the user to enter the seat pass wants to seat in. (See Below)
    colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))the if statement is neccessary to determine if seat has already been taken
    (this line of code does not work: need help
    If btnSeatResv(row pos - 1, colpos - 1).Enabled <> True Then
    MsgBox("Seat already taken. Please choose another seat")
    Else
    basically it alway skip the first part of if statement and goes to the else portion rather the seat has already been assigned or not.
    the next line of code assigns the pass name and desired seat location.
    SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
    it then assigns the location to the btnarray and change color from green to red and enables it. (see below)
    btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
    btnSeatResv(row pos - 1, colpos - 1).Enabled = False

    This works for the first passenger assignment but when I try to add another passenger it does not change the color of button and disable it, but it does assign to seating chart array

    Can anyone give my some suggestions to how to fix my code I need the project done by 11:59 pm tonight. Code is given agian but not broken up below

    I thank you all and appreiciate your suggestions

    (ARRAYS ARE DECLARED OUT SIDE OF THE btnAddPass procedure)


    Private Sub btnAddPass_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnAddPass.Clic k
    Dim row, row2 As Integer
    Dim col, col2 As Integer
    Dim x As Integer = 221
    Dim y As Integer = 150
    Dim a As Integer = 128
    Dim b As Integer = 153
    Dim c As Integer = 150
    Dim d As Integer = 130
    Dim rowpos As Integer
    Dim colpos As Integer

    Dim passenger As String
    AddButton(row, col, x, y)
    AddSeatLabel(co l2, c, d)
    AddRowNum(row2, a, b)
    passenger = txtPassName.Tex t
    rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
    colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))

    If btnSeatResv(row pos - 1, colpos - 1).Enabled <> True Then
    MsgBox("Seat already taken. Please choose another seat")
    Else
    SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
    btnSeatResv(row pos - 1, colpos - 1).Text = " "

    btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
    btnSeatResv(row pos - 1, colpos - 1).Enabled = False

    End If
    txtPassName.Cle ar()
    txtPassName.Foc us()

    End Sub
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by meena76
    I posted my problem in ref to my Airline Reservation Program. I am using VB 2005 for this program

    I have to right a program that req a users name for a text box then assigns the user a seat (row(x), seat(x). The seat assignment is based on a two dimensional array seatingchart(9, 3). When the user enters name in the text box a click event must be activated in order to store the passenger in a variable string passenger. I also have a control array btnseatresv(9,3 ) that display a 10 X 4 tabular table of button which signifies the seat location, if a seat is red then the seat is already taken if the seat is green then the seat is open. each button has a click event on it. I am having some problems with the logic behind the code.

    1. When the passenger name is typed in the text box and the addpassenger button is clicked it assigns the variable to the passenger name code below:

    passenger = txtPassName.Tex t

    then an input box is displayed which then ask the user to enter the row pass wants to seat in. (See Below)
    rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
    next another input box is displayed which then ask the user to enter the seat pass wants to seat in. (See Below)
    colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))the if statement is neccessary to determine if seat has already been taken
    (this line of code does not work: need help
    If btnSeatResv(row pos - 1, colpos - 1).Enabled <> True Then
    MsgBox("Seat already taken. Please choose another seat")
    Else
    basically it alway skip the first part of if statement and goes to the else portion rather the seat has already been assigned or not.
    the next line of code assigns the pass name and desired seat location.
    SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
    it then assigns the location to the btnarray and change color from green to red and enables it. (see below)
    btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
    btnSeatResv(row pos - 1, colpos - 1).Enabled = False

    This works for the first passenger assignment but when I try to add another passenger it does not change the color of button and disable it, but it does assign to seating chart array

    Can anyone give my some suggestions to how to fix my code I need the project done by 11:59 pm tonight. Code is given agian but not broken up below

    I thank you all and appreiciate your suggestions

    (ARRAYS ARE DECLARED OUT SIDE OF THE btnAddPass procedure)


    Private Sub btnAddPass_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnAddPass.Clic k
    Dim row, row2 As Integer
    Dim col, col2 As Integer
    Dim x As Integer = 221
    Dim y As Integer = 150
    Dim a As Integer = 128
    Dim b As Integer = 153
    Dim c As Integer = 150
    Dim d As Integer = 130
    Dim rowpos As Integer
    Dim colpos As Integer

    Dim passenger As String
    AddButton(row, col, x, y)
    AddSeatLabel(co l2, c, d)
    AddRowNum(row2, a, b)
    passenger = txtPassName.Tex t
    rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
    colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))

    If btnSeatResv(row pos - 1, colpos - 1).Enabled <> True Then
    MsgBox("Seat already taken. Please choose another seat")
    Else
    SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
    btnSeatResv(row pos - 1, colpos - 1).Text = " "

    btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
    btnSeatResv(row pos - 1, colpos - 1).Enabled = False

    End If
    txtPassName.Cle ar()
    txtPassName.Foc us()

    End Sub
    Hi could you show us how the arrays are declared as that seems to be important
    thanks

    Comment

    • meena76
      New Member
      • Nov 2006
      • 9

      #3
      First and foremost I want to say thank you I really appreciate any help you may be able to give.

      I have modified the code since that last posting,
      and my previous problem has been resolved
      i moved my subprocedures which calls the control arrays
      Dim btnSeatResv(9, 3) As Button
      Dim lblSeatNum(4) As Label
      Dim lblRowNum(10) As Label
      to form load and it works now.

      My new problem is: I need to be able to determine when a seat has already been assigned, prompt user to chose another seat, and then assign that seat it needs to continue this check until user finds and unsigned seat - I attempted this with the code below, but it has a problem with it.

      I added a do until loop to the following code (see below)
      if codition is true to exit loop it also exits the if statement, I know this is a simple fix but my brain is about fried now any suggestions to get it to execute the next line of code: to assign
      SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos

      Changed Code
      If btnSeatResv(row pos - 1, colpos - 1).Enabled = False Then
      do until btnSeatResv(row pos - 1, colpos - 1).Enabled = True
      MsgBox("Seat already taken. Please choose another seat")
      rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
      colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))
      Loop
      Else
      SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
      btnSeatResv(row pos - 1, colpos - 1).Text = "."

      btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
      btnSeatResv(row pos - 1, colpos - 1).Enabled = False
      End If



      Complete Code
      Dim btnSeatResv(9, 3) As Button
      Dim lblSeatNum(4) As Label
      Dim lblRowNum(10) As Label
      Dim lblrow As Label
      Dim SeatingChart(9, 3) As String
      Dim rowcount As Integer = 0
      Dim colcount As Integer = 0
      Dim upperbound As Integer
      Dim counter As Integer

      Private Sub btnAddPass_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnAddPass.Clic k

      Dim rowpos As Integer
      Dim colpos As Integer



      Dim passenger As String

      passenger = txtPassName.Tex t
      rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
      colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))

      If btnSeatResv(row pos - 1, colpos - 1).Enabled = False Then
      Do Until btnSeatResv(row pos - 1, colpos - 1).Enabled = True
      MsgBox("Seat already taken. Please choose another seat")
      rowpos = CInt(InputBox(" Please enter row passenger wants to seat in:"))
      colpos = CInt(InputBox(" Please enter seat passenger wants to seat in:"))
      Loop
      Else
      SeatingChart(ro wpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
      btnSeatResv(row pos - 1, colpos - 1).Text = "."

      btnSeatResv(row pos - 1, colpos - 1).BackColor = Color.Red
      btnSeatResv(row pos - 1, colpos - 1).Enabled = False

      End If
      txtPassName.Cle ar()
      txtPassName.Foc us()

      End Sub

      Sub AddButton(ByRef row As Integer, ByRef col As Integer, ByRef x As Integer, ByRef y As Integer)
      lblrow = New Label
      lblrow.Text = "Row"
      lblrow.Name = "lblrow"
      lblrow.Width = 50
      lblrow.Height = 20
      lblrow.TextAlig n = ContentAlignmen t.MiddleCenter
      lblrow.Location = New Point(156, 128)
      Controls.Add(lb lrow)


      For row = 0 To 9
      For col = 0 To 3
      btnSeatResv(row , col) = New Button
      btnSeatResv(row , col).Name = "btnSeatRes v" & row & col
      btnSeatResv(row , col).Text = ""
      btnSeatResv(row , col).Location = New Point(x, y)
      btnSeatResv(row , col).Enabled = True
      btnSeatResv(row , col).Width = 15
      btnSeatResv(row , col).Height = 20
      btnSeatResv(row , col).BackColor = Color.Green
      Controls.Add(bt nSeatResv(row, col))
      x += 44

      Next
      x = 221
      y += 25
      Next
      End Sub
      Sub AddSeatLabel(By Ref col2 As Integer, ByRef c As Integer, ByRef d As Integer)

      For col2 = 1 To 4
      c += 48
      lblSeatNum(col2 ) = New Label
      lblSeatNum(col2 ).Name = "txtSeatRes v" & col2
      lblSeatNum(col2 ).Text = "Seat " & col2
      lblSeatNum(col2 ).Width = 50
      lblSeatNum(col2 ).Height = 15
      lblSeatNum(col2 ).TextAlign = ContentAlignmen t.MiddleCenter
      lblSeatNum(col2 ).Location = New Point(c, d)
      Controls.Add(lb lSeatNum(col2))

      Next
      End Sub
      Sub AddRowNum(ByRef row2 As Integer, ByVal a As Integer, ByVal b As Integer)
      For row2 = 1 To 10
      a += 44
      lblRowNum(row2) = New Label
      lblRowNum(row2) .Name = "txtSeatRes v" & row2
      lblRowNum(row2) .Text = row2 & " :"
      lblRowNum(row2) .Width = 25
      lblRowNum(row2) .Height = 15
      lblRowNum(row2) .TextAlign = ContentAlignmen t.MiddleRight
      lblRowNum(row2) .Location = New Point(a, b)
      Controls.Add(lb lRowNum(row2))
      a = 128
      b += 25

      Next
      End Sub


      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
      Dim row, row2 As Integer
      Dim col, col2 As Integer
      Dim x As Integer = 221
      Dim y As Integer = 150
      Dim a As Integer = 128
      Dim b As Integer = 153
      Dim c As Integer = 150
      Dim d As Integer = 130
      AddButton(row, col, x, y)
      AddSeatLabel(co l2, c, d)
      AddRowNum(row2, a, b)
      End Sub
      End Class

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Here's something you can try (very tiny change)...
        Code:
        Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
        
        Dim rowpos As Integer
        Dim colpos As Integer
        
        Dim passenger As String
        
        passenger = txtPassName.Text
        rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
        colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
        
        If btnSeatResv(rowpos - 1, colpos - 1).Enabled = False Then
        Do Until btnSeatResv(rowpos - 1, colpos - 1).Enabled = True
          MsgBox("Seat already taken. Please choose another seat")
          rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
          colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
        Loop
        [B]End If[/B]
        SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
        btnSeatResv(rowpos - 1, colpos - 1).Text = "."
        
        btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
        btnSeatResv(rowpos - 1, colpos - 1).Enabled = False
        
        [B]'End If[/B]
        txtPassName.Clear()
        txtPassName.Focus()
        
        End Sub
        Also, I'd recommend indenting your code - makes it much easier to read. Or is it just messed up from pasting it here?

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Hi Meena76.

          (Hope you didn't miss my posting from a couple of minutes ago because of this one. I just wanted to make another comment.)

          While I realise this is kind of a "quick and dirty" application, and put together in a hurry, I'd suggest you validate (check) the input values before using them as indexes for your array. Otherwise, if the user enters a value outside of the proper range they'll crash your code.

          For example, what if the user accidentally requests row 5, seat number 33333 because their finger slipped?

          Comment

          • meena76
            New Member
            • Nov 2006
            • 9

            #6
            thank you for the suggesting, it is due to copy & pasteing code.

            yes this worked I new it was very simple thank you very much.

            I have another question.

            Before I added a click event to the buttons created in the control array.

            Is there a way to assign the positon based on what btn is clicked on to another array.

            in other words if user clicks btn located at btnresvseat(1,1 ) I want to then assign that same location to the seating chart(1,1). Is there a way for VB to refer to the row, col of a two dimensional array. If this is not clear I will try to explain better.

            thank you for your assistance

            Comment

            • meena76
              New Member
              • Nov 2006
              • 9

              #7
              yes i agree that is actually my next step and goal. wish me look I'm sure I will be back for more suggestions.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by meena76
                yes this worked I new it was very simple thank you very much.
                That's good news. :)

                Originally posted by meena76
                I have another question.

                Before I added a click event to the buttons created in the control array.

                Is there a way to assign the positon based on what btn is clicked on to another array.

                in other words if user clicks btn located at btnresvseat(1,1 ) I want to then assign that same location to the seating chart(1,1). Is there a way for VB to refer to the row, col of a two dimensional array. If this is not clear I will try to explain better.

                thank you for your assistance
                Hm...

                I'm going to assume that the array of buttons actually has only one dimensional, because I don't think it's possible to create a two-dimensional control array. Please tell me if I'm incorrect on this point - I certainly don't know everything about them.

                For the sake of argument, let's assume you have an array of command buttons called Button arranged in a 5 x 5 grid on the form. As a control array, the index is passed to the Click event. So, here's a quick sample I've just tested in VB6 and pasted over here.
                Code:
                Private Sub Button_Click(Index As Integer)
                  Const MaxRows As Long = 5
                  Const MaxSeats As Long = 5
                  Dim TheColumn As Long, TheRow As Long
                  TheRow = Int(Index / MaxRows)
                  TheColumn = Index - MaxRows * TheRow [b]+ 1[/b]
                  [B]TheRow = TheRow + 1[/B]
                  Debug.Print "Row:"; TheRow, "Seat:"; TheColumn
                End Sub
                My button array started at zero, of course, which is why I'm adding 1.

                Comment

                Working...