Problem with my VB program for class

Status
Not open for further replies.
Joined
Jun 3, 2005
Messages
907
I am in a VB programming class and I am having an issue with a program that I created. The focus of the program is creating a class to do calculations and sharing the data with a form.

The problem that I am having is with a certain piece of data that is shared between the class and form. To be more specific, it is two check boxes that each have their own variable as boolean, done as a true/false if checked/unchecked situation.

When I step through the program, somehow these variables get changed entering the class for calculations. For example if it was true, false, it comes out as false, true when the class pulls the data. There are other variables and data types that get pulled but these are the only ones that are being altered.

Any ideas?
 
Are the variables themselves changing, or are the values you are expecting changing?

Because whatever function is grabbing the data could have those swapped... so the data didn't really change but your output just had them swapped

Otherwise I would just look through your code and see where it could be modifed... you don't have any if(boolean = true) which would just assign true to that boolean without any warnings./errors Well actually I don't know the exact syntax for Visual Basic, I'm thinking it's close to C ...
 
The values get changed, I have stepped through my program and when the class accesses the data, the boolean values get changed.
 
Well... can't you step into the class itself and go through it? Didn't you write it?

I guess I'm confused... if it's your class that's doing work on the data, can't you just look at the code and see every possible place where it could be modified (Any line it's used really). Unless this is a massive project it shouldn't be too hard to debug.

I had some problems with templates last night in C++... I happened to declare a member variable in a class to be the same thing as one of the template parameters, but I was only using it in the constructor, not in the function that wouldn't compile. It would throw the most obscure errors (Borland, Microsoft and GNU compilers)... took an hour to figure out before someone helped me find that small mistake.
 
I don't see this thread quickly getting resolved without source code.
 
When the Sub New in my class is executed, that is when the values get switched, and after further testing, the values only get switched when they are not the same. True False ends up False True and the other way around, but True True stays True True and False False stays False False.

Here is my calculate button from my form

Code:
 Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

        ' Create Reservations object to connect to buisness tier

        Dim NightsDecimal As Decimal
        Dim RoomSizeString As String
        Dim Weekend As Boolean
        Dim AAA As Boolean

        ' Checks Name Box for text

        ErrorProvider1.Clear()

        If CustomerNameTextBox.Text <> "" Then

            'Checks for Credit Card Info

            If CreditCardNumberTextBox.Text <> "" Then
                If KingSizeRadioButton.Checked = True Then
                    RoomSizeString = "King"
                ElseIf QueenSizeRadioButton.Checked = True Then
                    RoomSizeString = "Queen"
                ElseIf DoubleSizeRadioButton.Checked = True Then
                    RoomSizeString = "Double"
                Else
                    Me.ErrorProvider1.SetError(RoomSizeGroupBox, "Must select room size")
                End If
                If WeekEndCheckBox.Checked = True Then
                    Weekend = True
                End If
                If AarpAaaCheckBox.Checked = True Then
                    AAA = True
                End If

                'Check to see if number is greater than 0

                If Decimal.TryParse(NumberOfNightsTextBox.Text, NightsDecimal) Then
                    Try
                        Dim aReservationsClass As New ReservationsClass(NightsDecimal, RoomSizeString, Weekend, AAA)
                        PriceOfStayTextBox.Text = aReservationsClass.StayPrice.ToString("C")

                    Catch ex As ApplicationException


                    End Try
                Else
                    Me.ErrorProvider1.SetError(NumberOfNightsTextBox, "Must Enter Number of Nights")
                    SelectControlInError(NumberOfNightsTextBox)
                End If
            Else
                Me.ErrorProvider1.SetError(CreditCardNumberTextBox, "Must Enter Credit Card Number")
                SelectControlInError(CreditCardNumberTextBox)
            End If
        Else
            Me.ErrorProvider1.SetError(CustomerNameTextBox, "Must Enter A Name")
            SelectControlInError(CustomerNameTextBox)
        End If
    End Sub

And here is the class

Code:
Code:
 Private RoomSizeString As String
    Private Nights As Decimal
    Private AARP As Boolean
    Private Weekend As Boolean
    Private Price As Decimal

    Private Const m_AARP As Decimal = 0.01D
    Private Const m_Tax As Decimal = 0.01D
    Private Const m_KingDay As Decimal = 100D
    Private Const m_KingEnd As Decimal = 100D
    Private Const m_QueenDay As Decimal = 85D
    Private Const m_QueenEnd As Decimal = 95D
    Private Const m_DoubleDay As Decimal = 69.95D
    Private Const m_DoubleEnd As Decimal = 79.95D

    Sub New(ByVal NightsDecimal As Decimal, ByVal RoomSizeString As String, ByVal AARP As Boolean, ByVal Weekend As Boolean)

        Me.RoomSizeString = RoomSizeString
        Me.Nights = NightsDecimal
        Me.AARP = AARP
        Me.Weekend = Weekend

        FindPrice()

    End Sub

    Private Sub FindPrice()
        Dim PreTax As Decimal
        Dim PreAARP As Decimal

        'calcuates reservation price

        If RoomSizeString = "King" Then
            If Weekend = True Then
                If AARP = True Then
                    PreAARP = Nights * m_KingEnd
                    PreTax = PreAARP - (PreAARP * m_AARP)
                    Price = PreTax + (PreTax * m_Tax)
                Else
                    PreTax = Nights * m_KingEnd
                    Price = PreTax + (PreTax * m_Tax)
                End If

            ElseIf Weekend = False And AARP = True Then
                PreAARP = Nights * m_KingDay
                PreTax = PreAARP - (PreAARP * m_AARP)
                Price = PreTax + (PreTax * m_Tax)
            Else
                PreTax = Nights * m_KingDay
                Price = PreTax + (PreTax * m_Tax)

            End If
        ElseIf RoomSizeString = "Queen" Then
            If Weekend = True Then
                If AARP = True Then
                    PreTax = Nights * m_QueenEnd
                    PreAARP = PreTax * m_AARP
                    Price = PreAARP * m_Tax
                Else
                    PreTax = Nights * m_QueenEnd
                    Price = PreTax * m_Tax
                End If
            ElseIf Weekend = False And AARP = True Then
                PreTax = Nights * m_QueenDay
                PreAARP = PreTax * m_AARP
                Price = PreAARP * m_Tax
            Else
                PreTax = Nights * m_QueenDay
                Price = PreTax * m_Tax
            End If
        ElseIf RoomSizeString = "Double" Then
            If Weekend = True Then
                If AARP = True Then
                    PreTax = Nights * m_DoubleEnd
                    PreAARP = PreTax * m_AARP
                    Price = PreAARP * m_Tax
                Else
                    PreTax = Nights * m_DoubleEnd
                    Price = PreTax * m_Tax
                End If
            ElseIf Weekend = False And AARP = True Then
                PreTax = Nights * m_DoubleDay
                PreAARP = PreTax * m_AARP
                Price = PreAARP * m_Tax
            Else
                PreTax = Nights * m_DoubleDay
                Price = PreTax * m_Tax
            End If
            End If
    End Sub

    Public ReadOnly Property StayPrice As Decimal
        Get
            Return Price
        End Get
    End Property

End Class
 
When the Sub New in my class is executed, that is when the values get switched, and after further testing, the values only get switched when they are not the same. True False ends up False True and the other way around, but True True stays True True and False False stays False False.
What happens when you breakpoint and step through the code? You can setup a "watch" on a variable, and know exactly when it's being changed. Hovering over variables also works, too. You've identified particular cases when values will switch and will not switch, so what does that tell you about some of your conditional and class logic?


And slightly off-topic... change this code:
Code:
If WeekEndCheckBox.Checked = True Then
    Weekend = True
End If

... to be this instead:
Code:
Weekend = WeekEndCheckBox.Checked

(This also applies for the other similar variable assignments.)
 
Last edited:
Here's your problem. Or, one of them at least (some serious house cleaning required there). They don't get swapped, you pass them in the wrong order.
Code:
 Dim aReservationsClass As New ReservationsClass(NightsDecimal, RoomSizeString, [B]Weekend[/B], [B]AAA[/B])
<snip>
 Sub New(ByVal NightsDecimal As Decimal, ByVal RoomSizeString As String, ByVal [B]AARP[/B] As Boolean, ByVal [B]Weekend[/B] As Boolean)
 
Last edited:
Thanks everyone for the assistance. I would have like to written better, more efficient code but my learning/understanding of VB or programming in general is not very good and is a very torturous and laborious task for me. This class was a requirement for me to take.

Lunknumb had the answer to fix my problem. I was totally unaware that they all had to be listed in the same order.
 
Status
Not open for further replies.
Back
Top