[VB] Uncertain why this isn't working

Started by
2 comments, last by Tim Lawton 10 years, 9 months ago

Hey,

I've just written this code, unfortunately the 'price' & 'price2' variables always returns as 0 when I print it, and I'm really confused why as it should, also instead of it adding the value to price during the if statements, it just goes straight to the last Else which prints out that I should enter a correct numeric value



        Dim theHeight As String = heightEntry.Text
        Dim theWidth As String = widthEntry.Text
        Dim price As Integer
        Dim price2 As Integer

        If IsNumeric(theWidth) >= 1800 Then
            price = 1444

        ElseIf IsNumeric(theWidth) >= 1801 And IsNumeric(theWidth) <= 2000 Then
            price = 1458

        ElseIf IsNumeric(theWidth) >= 2001 And IsNumeric(theWidth) <= 2200 Then
            price = 1479

        ElseIf IsNumeric(theWidth) >= 2201 And IsNumeric(theWidth) <= 2400 Then
            price = 1580

        ElseIf IsNumeric(theWidth) >= 2401 And IsNumeric(theWidth) <= 2600 Then
            price = 1606

        ElseIf IsNumeric(theWidth) >= 2601 And IsNumeric(theWidth) <= 2800 Then
            price = 1630

        ElseIf IsNumeric(theWidth) >= 2801 And IsNumeric(theWidth) <= 3000 Then
            price = 2185

        ElseIf IsNumeric(theWidth) >= 3001 And IsNumeric(theWidth) <= 3200 Then
            price = 2204

        ElseIf IsNumeric(theWidth) >= 3201 And IsNumeric(theWidth) <= 3400 Then
            price = 2259
        Else
            Output("Please Enter Numbers Between 1800 to 3400")
        End If

        If IsNumeric(theHeight) > 2100 Then
            price2 = price * 1.05
        Else
            price2 = price
        End If

        Output("Value is: " + price2.ToString)

    End Sub

Any help would be greatly appreciated, thanks!

Advertisement

IsNumeric() doesn't convert its argument to a number, it only tells you if the argument is a valid number or not. Depending on exactly which version of VB you're using and what you want to convert the argument to then you might want to try CInt(), CLng(), CDbl() or similar functions instead.

Can't you just use theWidth as a number anyway in VB? (as long as IsNumeric is true)

So

If IsNumeric(theWidth) Then

' do all that stuff you already doing, but you don't need IsNumeric any more

Else

Output("Please Enter Numbers Between 1800 to 3400")

Endif

Otherwise I think you can do

Dim price as Integer = theWidth

as well?

(Disclaimer: I hate VB and the syntax sucks so I could be wrong)

EDIT: And I haven't tried any .NET VB stuff, last time I did VB was VB6, so it may have changed completely now!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Thanks SiCrane, worked like a charm!

This topic is closed to new replies.

Advertisement