Alli OGrady
Alli OGrady

Reputation: 287

Calculation Issue in VB 2010

I am having trouble figuring out where my issue with this code is. My calculations work perfectly until the last case (CASE IS > 8), which continuously returns a 0.00 as the result. I'm sure its something small that a newbie like me is missing due to lack of experience. Thank you for your help!

' Declaration of Variable

Convert.ToInt32(txtAttending.Text)
Dim decAttending = txtAttending.Text

If IsNumeric(txtAttending.Text) And txtAttending.Text <= 16 Then
    Select Case txtAttending.Text
        Case Is = 1
            decCost = 695 * decAttending
        Case 2 To 4
            decCost = 545 * decAttending
        Case 5 To 8
            decCost = 480 * decAttending
        Case Is > 8
            decCost = 395 * decAttending
    End Select
Else
    MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")

End If

If radYes.Checked = True Then
    decFinalCost = (decCost - (decCost * 0.15))
    lblRepeatDiscount.Visible = True
    decDiscount = (decCost * 0.15)
    lblDiscount.Text = decDiscount.ToString("C")
    lblTotalPrice.Text = decFinalCost.ToString("C")

Else
    decFinalCost = decCost
    lblTotalPrice.Text = decFinalCost.ToString("C")
End If

Upvotes: 2

Views: 221

Answers (4)

md nth
md nth

Reputation: 119

Apart from mentioned advices, it seems great, but are you sure that all code you did is written, or is there a thrown code that manipulate Diccost,or disattending,please assure us that.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Case Else
        decCost = 395 * decAttending

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

Shouldn't you convert the text to a number first? I'd change these lines:

If IsNumeric(txtAttending.Text) And Val(txtAttending.Text) <= 16 Then
    Select Case Val(txtAttending.Text)

Notice that this snippet has no effect: Convert.ToInt32(txtAttending.Text), you're converting a text to an integer, but the result is not being stored in a variable.

Also, what happens if the number is <= 0 ?

Upvotes: 1

Jacob Krall
Jacob Krall

Reputation: 28825

Convert.ToInt32(txtAttending.Text) does not convert the text in-place, as your first line of code seems to assert; this is a no-op. txtAttending.Text has type System.String, and will always have that type.

Your case statement should look more like this:

If IsNumeric(txtAttending.Text) Then

    Dim decAttending = Convert.ToInt32(txtAttending.Text)

    Select Case decAttending
        Case Is = 1
            decCost = 695 * decAttending
        Case 2 To 4
            decCost = 545 * decAttending
        Case 5 To 8
            decCost = 480 * decAttending
        Case 9 To 16
            decCost = 395 * decAttending
    End Select
Else
    MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")

This version always uses the System.Int32 variable decAttending when doing numeric operations.

Upvotes: 4

Related Questions