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

I took the liberty of changing your post type from "Code Snippet" to "Discussion Thread". Code Snippets are for when you are posting working code. When you post code with a question you should use the default of "Discussion Thread". I know, it's confusing because when you add code the edit window says "code snippet".

A little terminology note - "literals" are values that are not assigned to variables as in

If mynum >= 5 Then

where 5 is the literal. It's generally frowned upon because 5 is not descriptive whereas

Const MAXTICKETS = 5

If mynum >= MAXTICKETS Then

means something to someone reading the code. But enough preaching. Can you please post the non-working code so I can see what doesn't work?

As you can see, I changed all the local variables--> decTotalCost to be a class variable--> _decTotalCost and I create a class variable --> _strTotalCost that uses the value of _decTotalCost to populate the text element of the feedback label and now it reads as $0.00 again when I run the application. I don't understand why it doesn't work and would like some assistance.

' 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 _decTotalCost As Decimal = 0D


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 _strTotalCost As String = "The Total Cost of Tickets Purchased is: " & _decTotalCost.ToString("C")





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()


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()
    ' 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 = _strTotalCost
            lblTotalCost.Visible = True




        End If

    End If

End Sub

End Class

In the non-working code you display the total (line 315) from _strTotalCost, however, aside from initializing this string in line 22 you never actually copied the numeric total into the string so it was only ever going to display the string set at line 22. At that point _decTotalCost has the value 0.

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

I thought that these lines--187 to 210 did that though. This is why I can't figure out why it says zero. I understand that _decTotalCost has a value of zero when it is displayed in _strTotalCost but These statements should have added value to that variable right?

Those lines modify the value of _decTotalCost but you still need to add code to convert that to a string value in _strTotalCost. You have to do that EVERY time you assign a new value to _decTotalCost.

Thank you very much. I totally missed that. I appreciate you help figuring that out.

No problem. Glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.