我目前正在为使用Framework 4.0的电影院设计客户预订系统的模型。目前的所有数据都将存储在一个阵列中,因为它是一个模型,它将保留它们(客户名称,票价等),因为此时不需要永久存储数据。在VB.Net中将变量写入预先存在的数组
我有一个GUI窗体和两个主要类,MainForm.vb来处理应用程序的I/O功能,SeatManager.vb来管理要运行的背景方法以使程序运行。每个类的代码如下: -
**MainForm.vb Class**
Public Class MainForm
Private Const m_totalNumberOfSeats As Integer = 60
Private m_seatManager As SeatManager
Public Sub New()
InitializeComponent()
m_seatManager = New SeatManager(m_totalNumberOfSeats)
InitializeGUI()
End Sub
''' <summary>
''' Method called from the MainForm() method. This method is called when the form
''' is opened by the program (on initialisation).
''' </summary>
Private Sub InitializeGUI()
rbtnReservation.Checked = True 'Sets the Reserve button as being chosen
lstReservations.Items.Clear() 'Clears the list displaying all seats and reservations
txtCustomerName.Text = String.Empty 'Sets the name textbox as emtpy
txtSeatPrice.Text = String.Empty
cmbDisplayOptions.Items.AddRange([Enum].GetNames(GetType(SeatManager.DisplayOptions)))
cmbDisplayOptions.SelectedIndex = SeatManager.DisplayOptions.AllSeats
For i As Integer = 0 To m_totalNumberOfSeats - 1
lstReservations.Items.Add(GetMyString(i))
Next
End Sub
Private Function GetMyString(ByVal i As Integer) As String
If i >= 0 AndAlso i < m_totalNumberOfSeats Then
Return String.Format("{0}{1}{2}{3}", GetPaddedString(m_seatManager.MyArray(i, 0), 29), _
GetPaddedString(m_seatManager.MyArray(i, 1), 41), _
GetPaddedString(m_seatManager.MyArray(i, 2), 63), _
m_seatManager.MyArray(i, 3))
Else
Return String.Empty
End If
End Function
Private Function GetPaddedString(ByVal o As Object, ByVal length As Integer) As String
Dim s As String = String.Empty
If o IsNot Nothing Then s = o.ToString()
If s.Length > length Then s = s.Substring(0, length)
Return s.PadRight(length - s.Length)
End Function
'--Event Handler when you change the value of Customer Name/Price
' Dim index As Integer = lstBox.selectedIndex
'm_arrayClass.PopulateArray(index,customerName.text, ctype(price.text,double))
'lstBox.Items(index) = GetMyString(index)
'Private Function CheckSelectedIndex() As Boolean
' If lstReservations.SelectedIndex <= 0 Then
' Return lstReservations.SelectedIndex
' Else
' Return False
' End If
'End Function
''' <summary>
''' Method uses to verify that the user has entered text within the txtName textbox
''' </summary>
''' <param name="name">String variable passing the customer name inputted by the user</param>
''' <returns>True if validation confirms user has entered at least one character in
''' the txtName field otherwise returns False. If returns false, an error message will
''' be displayed to the user</returns>
Private Function ReadAndValidateName(ByRef name As String) As Boolean
If (String.IsNullOrEmpty(txtCustomerName.Text)) Or (String.IsNullOrEmpty(txtCustomerName.Text)) Then
MessageBox.Show("You have not entered a valid customer name. Please try again", _
"Invalid Customer Name", MessageBoxButtons.OK)
txtCustomerName.Focus()
txtCustomerName.SelectAll()
Return False
Else
Return True
End If
End Function
''' <summary>
''' This method calls the GetDouble() method in the InputUtility class to convert the text
''' given by the user in the txtPrice textbox.
''' </summary>
''' <param name="seatPrice">Output parameter receiving the converted value</param>
''' <returns>True if conversion is successful and validates or False. If returns False
''' an error message will be displayed to the user.</returns>
Private Function ReadAndValidatePrice(ByRef seatPrice As Double) As Boolean
If (InputUtility.GetDouble(txtSeatPrice.Text, seatPrice) And (seatPrice >= 0)) Then
Return True
Else
MessageBox.Show("You have not entered a valid price. Please try again", _
"Invalid Price", MessageBoxButtons.OK)
txtSeatPrice.Focus()
txtSeatPrice.SelectAll()
Return False
End If
End Function
''' <summary>
''' This method calls the above two methods (validate name and validate price) and if both return true,
''' this method also returns true. If either of the above two methods are false, this method returns false
''' </summary>
''' <param name="name">Output Parameter - customer name</param>
''' <param name="seatPrice">Output parameter - seat price</param>
''' <returns>Returns true if validates and false if not</returns>
Private Function ReadAndValidateInput(ByRef name As String, ByRef seatPrice As Double) As Boolean
Dim nameResult As Boolean = ReadAndValidateName(name)
Dim priceResult As Boolean = ReadAndValidatePrice(seatPrice)
Return ((nameResult) And (priceResult))
End Function
Private Sub btnExitApplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitApplication.Click
Dim msgConfirmation As Integer
msgConfirmation = MessageBox.Show("Are You Sure You Wish To Exit This Application?", "WARNING!", _
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
If msgConfirmation = vbYes Then
Application.Exit()
End If
End Sub
End Class
**SeatManager.vb Class**
Public Class SeatManager
Private _array(,) As Object
Dim m_totalNumberOfSeats As Integer
Public Sub New(ByVal maxNumberOfSeats As Integer)
m_totalNumberOfSeats = maxNumberOfSeats
ReDim _array(m_totalNumberOfSeats - 1, 3)
For i As Integer = 0 To m_totalNumberOfSeats - 1
PopulateArray(i, Nothing, 0.0)
Next
End Sub
Public ReadOnly Property MyArray() As Object(,)
Get
Return _array
End Get
End Property
Public Enum DisplayOptions
AllSeats
VacantSeats
ReservedSeats
End Enum
Public Sub PopulateArray(ByVal i As Integer, ByVal CustomerName As String, ByVal Price As Double)
Dim av As String = "Available"
If Not String.IsNullOrEmpty(CustomerName) Then av = "Not Available"
'Did you say this has to be handled in an enum?
_array(i, 0) = i + 1 'Seat Number
_array(i, 1) = av
_array(i, 2) = CustomerName
_array(i, 3) = Price
End Sub
End Class
我寻找实现现在的问题是,我有一个名为文本框,txtCustomerName和一个叫txtPrice文本框。当用户在这些框中输入名称和价格并在列表框中选择一个条目时,lstReservations,我希望状态从可用更改为“保留”,并将客户名称和价格输入到相应的数组字段中。
有什么建议吗?在此先感谢
不幸的是,您在这里给我们提供了太多的代码。你能隔离问题区域,只显示解释问题所需的尽可能多的代码。 –
另外,即使你说你没有要求保存数据,我真的建议你看看某种形式的数据持久性,无论是XML还是数据库(SQL Express和SQLite都是免费的)或其他东西。使用数组有时会让你疯狂,而使用多维数组可能会让你更疯狂!你遇到的问题将很容易通过数据库解决。如果你要坚持非持久化,那么至少要查看'System.Collections'下的集合,比如'List(Of T)'。 –
我同意克里斯:使用List(T)填充表示实体(预订类,Customer类等)的类实例,而不是二维数组。即使是一个模型,这也会更容易。 –