473,466 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Two dimension Arrays

9 New Member
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.Text

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(rowpos - 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(rowpos - 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(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 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_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
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(col2, c, d)
AddRowNum(row2, a, b)
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 <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
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

End If
txtPassName.Clear()
txtPassName.Focus()

End Sub
Nov 7 '06 #1
7 6493
willakawill
1,646 Top Contributor
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.Text

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(rowpos - 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(rowpos - 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(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 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_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
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(col2, c, d)
AddRowNum(row2, a, b)
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 <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
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

End If
txtPassName.Clear()
txtPassName.Focus()

End Sub
Hi could you show us how the arrays are declared as that seems to be important
thanks
Nov 8 '06 #2
meena76
9 New Member
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(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos

Changed Code
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
Else
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
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_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
Else
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

End If
txtPassName.Clear()
txtPassName.Focus()

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.TextAlign = ContentAlignment.MiddleCenter
lblrow.Location = New Point(156, 128)
Controls.Add(lblrow)


For row = 0 To 9
For col = 0 To 3
btnSeatResv(row, col) = New Button
btnSeatResv(row, col).Name = "btnSeatResv" & 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(btnSeatResv(row, col))
x += 44

Next
x = 221
y += 25
Next
End Sub
Sub AddSeatLabel(ByRef 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 = "txtSeatResv" & col2
lblSeatNum(col2).Text = "Seat " & col2
lblSeatNum(col2).Width = 50
lblSeatNum(col2).Height = 15
lblSeatNum(col2).TextAlign = ContentAlignment.MiddleCenter
lblSeatNum(col2).Location = New Point(c, d)
Controls.Add(lblSeatNum(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 = "txtSeatResv" & row2
lblRowNum(row2).Text = row2 & " :"
lblRowNum(row2).Width = 25
lblRowNum(row2).Height = 15
lblRowNum(row2).TextAlign = ContentAlignment.MiddleRight
lblRowNum(row2).Location = New Point(a, b)
Controls.Add(lblRowNum(row2))
a = 128
b += 25

Next
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 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(col2, c, d)
AddRowNum(row2, a, b)
End Sub
End Class
Nov 8 '06 #3
Killer42
8,435 Recognized Expert Expert
Here's something you can try (very tiny change)...
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
  2.  
  3. Dim rowpos As Integer
  4. Dim colpos As Integer
  5.  
  6. Dim passenger As String
  7.  
  8. passenger = txtPassName.Text
  9. rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
  10. colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
  11.  
  12. If btnSeatResv(rowpos - 1, colpos - 1).Enabled = False Then
  13. Do Until btnSeatResv(rowpos - 1, colpos - 1).Enabled = True
  14.   MsgBox("Seat already taken. Please choose another seat")
  15.   rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
  16.   colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
  17. Loop
  18. End If
  19. SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
  20. btnSeatResv(rowpos - 1, colpos - 1).Text = "."
  21.  
  22. btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
  23. btnSeatResv(rowpos - 1, colpos - 1).Enabled = False
  24.  
  25. 'End If
  26. txtPassName.Clear()
  27. txtPassName.Focus()
  28.  
  29. End Sub
  30.  
Also, I'd recommend indenting your code - makes it much easier to read. Or is it just messed up from pasting it here?
Nov 8 '06 #4
Killer42
8,435 Recognized Expert Expert
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?
Nov 8 '06 #5
meena76
9 New Member
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
Nov 8 '06 #6
meena76
9 New Member
yes i agree that is actually my next step and goal. wish me look I'm sure I will be back for more suggestions.
Nov 8 '06 #7
Killer42
8,435 Recognized Expert Expert
yes this worked I new it was very simple thank you very much.
That's good news. :)

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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button_Click(Index As Integer)
  2.   Const MaxRows As Long = 5
  3.   Const MaxSeats As Long = 5
  4.   Dim TheColumn As Long, TheRow As Long
  5.   TheRow = Int(Index / MaxRows)
  6.   TheColumn = Index - MaxRows * TheRow + 1
  7.   TheRow = TheRow + 1
  8.   Debug.Print "Row:"; TheRow, "Seat:"; TheColumn
  9. End Sub
My button array started at zero, of course, which is why I'm adding 1.
Nov 8 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John Pastrovick | last post by:
Sort of sizeof but applied to a particular array dimension. Similar to ASP ubound(array,dimension)
9
by: James | last post by:
Hi, I am new to C++. I want to directly create a dynamic two-dimension double array, i.e. double pp. I found the "new" is only for one-dimension array, i.e. double *p = new p. How to "new" a...
6
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
3
by: tg | last post by:
How can I sort a two-dimension array on the first dimension in the array? If there's not a way to do it, how can I return the values of the first dimension so that I can sort the values myself...
2
by: Nathan Sokalski | last post by:
I have a multidimensional array declared as the following: Dim guesses(14, 5) As Integer I want to assign all values in a specific dimension to another array declared as follows:
5
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
4
by: Atemporal | last post by:
hi, all, i got some problem when define and use large dynamic two dimension arrays. At first, i use vector<vector<double>p1, i have three such arrays and each is around 10000*10000, the program...
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.