Ok, here is my question. I got this code working (Yeah it isn't really complex I know, but I'm a beginner anyway) and I couldn't make it work when using a class variable for the strTotalCost and decTotalCost variables and passing decTotalCost to the strTotalCost to show the calculated price. Could anyone tell me why when I replaced the class/global variables with the local variables it worked? What was happening prior was the final price was displaying as $0 every time. I tried If Then statements and Select Case statements and some combinations of the two to do the calculations, but nothing worked until I used the local variables. I know that it makes it simpler (and in this case more sense) to use the local rather than class variables, but I would like to know why my attemp at using class decTotalPrice and strTotalPrice resulted in the lblTotalCost displaying $0 because I would like to learn from my mistakes. Thank you for any help you can give me.
' Restrict uncoded data type changes (conversions)
Option Strict On
Public Class frmBaseballTicketSales
'Class variables (all Integers will be initialized as Integer Literals)
' (the Decimal will be initialized as a Decimal Literal)
Private _intSeasonBoxSeats As Integer = 2500
Private _intSeasonLowerDeck As Integer = 1500
Private _intSingleBoxSeats As Integer = 55
Private _intSingleLowerDeck As Integer = 35
Private _intSingleUpperDeck As Integer = 25
Private _intSingleStandingRoomOnly As Integer = 15
Private _intSeatType As Integer = -1
Private _strSeasonBoxSeats As String = "Box Seats $2500"
Private _strSeasonLowerDeck As String = "Lower Deck $1500"
Private _strSingleBoxSeats As String = "Box Seats $55"
Private _strSingleLowerDeck As String = "Lower Deck $35"
Private _strSingleUpperDeck As String = "Upper Deck $25"
Private _strSingleStandingRoomOnly As String = "Standing Room Only $15"
Private Sub frmBaseballTicketSales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This code will execute when the program loads. It will display the opening
' SplashScreen for 5 seconds allowing the user to view the program information.
Threading.Thread.Sleep(5000)
End Sub
Private Sub cboTicketChoice_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTicketChoice.SelectedIndexChanged
' This code executes when the user selects an item from the cboTicketChoice object.
' It calls the appropriate procedures to fill the lstSeatType object which will make
' visible some objects on the Windows form and fill the lstSeatType object with the
' appropriate string values.
' Declaration of variables.
Dim intTicketChoice As Integer = -1
intTicketChoice = Me.cboTicketChoice.SelectedIndex()
If intTicketChoice <> -1 Then
If intTicketChoice = 0 Then
SeasonTickets()
Else
SingleTickets()
End If
End If
End Sub
Sub SeasonTickets()
' This sub procedure will make visible the necessary objects and fill the Listbox Object
' with the appropriate items prior to handing control back to the calling procedure.
' Clear form for new choices.
txtNumberOfSeats.Text = ""
lstSeatType.Items.Clear()
' Make visible the appropriate objects.
lblNumberOfTickets.Visible = True
lblSeatType.Visible = True
txtNumberOfSeats.Visible = True
btnComputeTicketCost.Visible = True
btnClearForm.Visible = True
' Populate the ListBox Object.
lstSeatType.Items.Add(_strSeasonBoxSeats)
lstSeatType.Items.Add(_strSeasonLowerDeck)
' Make visible the ListBox Object.
lstSeatType.Visible = True
End Sub
Sub SingleTickets()
' This sub procedure will make visible the necessary objects and fill the Listbox Object
' with the appropriate items prior to handing control back to the calling procedure.
' Clear form for new choices.
txtNumberOfSeats.Text = ""
lstSeatType.Items.Clear()
' Make visible the appropriate objects.
lblNumberOfTickets.Visible = True
lblSeatType.Visible = True
txtNumberOfSeats.Visible = True
btnComputeTicketCost.Visible = True
btnClearForm.Visible = True
' Populate the ListBox Object.
lstSeatType.Items.Add(_strSingleBoxSeats)
lstSeatType.Items.Add(_strSingleLowerDeck)
lstSeatType.Items.Add(_strSingleUpperDeck)
lstSeatType.Items.Add(_strSingleStandingRoomOnly)
' Make visible the ListBox Object.
lstSeatType.Visible = True
End Sub
Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click
' This code executes when the user clicks the btnClearForm object.
' It resets the Windows form to the state at which it loaded at so
' the user can select some new choices.
cboTicketChoice.Text = "Select Ticket Type:"
txtNumberOfSeats.Text = ""
lstSeatType.Items.Clear()
lstSeatType.Visible = False
btnClearForm.Visible = False
btnComputeTicketCost.Visible = False
lblNumberOfTickets.Visible = False
lblSeatType.Visible = False
txtNumberOfSeats.Visible = False
btnComputeTicketCost.Visible = False
btnClearForm.Visible = False
lblTotalCost.Visible = False
End Sub
Private Sub btnComputeTicketCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeTicketCost.Click
' This code executes when the user clicks the btnComputeTicketCost Object.
' It will first call error handling functions to validate the user entered
' information. Next, if the information entered was valid, it will calculate
' total cost for the tickets chosen by the user and display that cost to the user
' in the lblTotalCost Object.
' Declare variables
Dim blnValidSeats As Boolean = False
Dim blnValidTickets As Boolean = False
Dim intTicketChoice As Integer = -1
Dim intSeatChoice As Integer = -1
Dim intSeatType As Integer = -1
Dim intNumberOfTickets As Integer = 0
Dim decTotalCost As Decimal
' Call functions to validate user entries
blnValidSeats = ValidateSeatNumber(intSeatChoice)
blnValidTickets = ValidateSeatType(intSeatType)
' If user entries are valid calculate the seat cost
If (blnValidSeats And blnValidTickets) Then
intTicketChoice = Me.cboTicketChoice.SelectedIndex
intSeatChoice = Me.lstSeatType.SelectedIndex
intNumberOfTickets = Convert.ToInt32(txtNumberOfSeats.Text)
'Calculate seat costs based on user provided information
If intTicketChoice = 0 Then
Select Case intSeatChoice
Case 0
decTotalCost = intNumberOfTickets * _intSeasonBoxSeats
Case 1
decTotalCost = intNumberOfTickets * _intSeasonLowerDeck
End Select
End If
If intTicketChoice = 1 Then
Select Case intSeatChoice
Case 0
decTotalCost = intNumberOfTickets * _intSingleBoxSeats
Case 1
decTotalCost = intNumberOfTickets * _intSingleLowerDeck
Case 2
decTotalCost = intNumberOfTickets * _intSingleUpperDeck
Case 3
decTotalCost = intNumberOfTickets * _intSingleStandingRoomOnly
End Select
End If
End If
DisplayCost(decTotalCost)
End Sub
Function ValidateSeatNumber(ByVal intSeats As Integer) As Boolean
' This code checks to see if the txtNumberOfSeats Object has a
' valid value. If not it gives feedback to the user to assist
' in correcting the problem.
Dim blnSeatsValid As Boolean = True
Try
intSeats = Convert.ToInt32(txtNumberOfSeats.Text)
Catch Exception As ArgumentNullException
MsgBox("Please enter a number of tickets", , "Number of tickets missing")
txtNumberOfSeats.Text = ""
txtNumberOfSeats.Focus()
blnSeatsValid = False
Catch Exception As FormatException
MsgBox("Please enter a number of tickets", , "Non numerical value")
txtNumberOfSeats.Text = ""
txtNumberOfSeats.Focus()
blnSeatsValid = False
Catch Exception As NullReferenceException
MsgBox("Please enter a number of tickets", , "Invalid number of tickets")
txtNumberOfSeats.Text = ""
txtNumberOfSeats.Focus()
blnSeatsValid = False
Catch Exception As SystemException
MsgBox("Please enter a number of tickets", , "Invalid number of tickets")
txtNumberOfSeats.Text = ""
txtNumberOfSeats.Focus()
blnSeatsValid = False
End Try
Return blnSeatsValid
End Function
Function ValidateSeatType(ByVal intSelection As Integer) As Boolean
' This code performs Error Handling to ensure that the lstSeatType has
' a valid entry. If not it will help the user to correct the problem.
Dim blnSeatType As Boolean = True
Try
intSelection = Me.lstSeatType.SelectedIndex
If intSelection = -1 Then
MsgBox("Please select a seat type", , "No Seat Type:")
End If
Catch Exception As ArgumentNullException
MsgBox("Please select a seat type", , "No seat type")
blnSeatType = False
Catch Exception As SystemException
MsgBox("Please select a seat type", , "Error in seat type")
blnSeatType = False
End Try
Return blnSeatType
End Function
Sub DisplayCost(ByVal decCost As Decimal)
' This sub procedure will display the cost of tickets if all information
' is filled in by the user. Or it will display an error message if this is not true
' and will attempt to help the user correct the issue.
If txtNumberOfSeats.Text <> "" Then
If lstSeatType.SelectedIndex <> -1 Then
lblTotalCost.Text = "The Total Cost of Tickets Purchased is: " & decCost.ToString("C")
lblTotalCost.Visible = True
End If
End If
End Sub
End Class