Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how can I combined these if statements into ONEelse if statement


If IsNumeric(txtEnterNumber.Text) Then
Number = CInt(txtEnterNumber.Text)

If Number >= 1 And Number <= 3999 Then
Number = CInt(txtEnterNumber.Text)
Else
MessageBox.Show("Please only enter a number within 1 and 3,999.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()

Exit Sub

End If

Else
MsgBox("Please enter a number in the textbox field, not letters or words.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()
Exit Sub
End If

If txtEnterNumber.Text.Contains(".") Then
MessageBox.Show("Please only enter whole numbers, not decimal numbers.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()
Exit Sub
End If


Dim Thousands As Integer = CInt(Math.Floor(Number / 1000) * 1000)
Dim Hundreds As Integer = CInt(Math.Floor((Number - Thousands) / 100) * 100)
Dim Tens As Integer = CInt(Math.Floor((Number - Hundreds - Thousands) / 10) * 10)
Dim Ones As Integer = Number Mod 10

Dim EnglishResult As String = ""

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    :
    : If IsNumeric(txtEnterNumber.Text) Then
    : Number = CInt(txtEnterNumber.Text)
    :
    : If Number >= 1 And Number <= 3999 Then
    : Number = CInt(txtEnterNumber.Text)
    : Else
    : MessageBox.Show("Please only enter a number within 1 and
    : 3,999.")
    : txtEnterNumber.Focus()
    : txtEnterNumber.Clear()
    :
    : Exit Sub
    :
    : End If
    :
    : Else
    : MsgBox("Please enter a number in the textbox field, not
    : letters or words.")
    : txtEnterNumber.Focus()
    : txtEnterNumber.Clear()
    : Exit Sub
    : End If
    :
    : If txtEnterNumber.Text.Contains(".") Then
    : MessageBox.Show("Please only enter whole numbers, not
    : decimal numbers.")
    : txtEnterNumber.Focus()
    : txtEnterNumber.Clear()
    : Exit Sub
    : End If
    :
    :
    : Dim Thousands As Integer = CInt(Math.Floor(Number / 1000) *
    : 1000)
    : Dim Hundreds As Integer = CInt(Math.Floor((Number -
    : Thousands) / 100) * 100)
    : Dim Tens As Integer = CInt(Math.Floor((Number - Hundreds -
    : Thousands) / 10) * 10)
    : Dim Ones As Integer = Number Mod 10
    :
    : Dim EnglishResult As String = ""
    :
    :

    Basically, you just look at the logic of the code. First you want the user to enter only a number. Then you want to check whether it's actually an Integer. With a 'flat-If-structure' the order of the different checks is very important (and sometimes you need to take the complement of the checks you would make when using nested If's). I can't really explain how to do it, other then to say: look at the logic you want to check. Then create If conditions that are perhaps too long (checking a lot of conditions multiple times). Then reorder such that you only need to check everything once (or as little as possible). Applying that to your If-structure above:
    [code]
    If Not IsNumeric(txtEnterNumber.Text) Then
    ... -> Not a number
    ElseIf txtEnterNumber.Text.Contains(".") Then
    ... -> Decimal number, not integer
    ElseIf CInt(txtEnterNumber.Text) >= 1 And CInt(txtEnterNumber.Text) <= 3999 Then
    ... -> Valid number, yay!
    Else
    ... -> Integer number, but value out of range
    End If[/code]


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.