1

I need help with a little problem. I haven't been programming long and can't figure out how to fix this. I have a small project for school and I can't figure out why I'm getting this error when validating inputs.

For example, I'm checking if the input is valid and between a min and max range by creating a function to return a true or false value based on the values entered. The reason I'm using a function is because I'm doing multiple similar checks and I figured rather than rewriting it out again, this was the best way to do this task.

Do While inputValid(string, min, max)

This is my validation, below is the simple function to validate this.

Private Function inputValid(input As String, min As Integer, max As Integer)
    If Not IsNumeric(input) Then
        Return False
    End If

    If input > min Or input < max Then
        Return False
    Else
        Return True
    End If
End Function

For some reason, despite the fact it should make sure that the value is numeric before it checks whether it's within a numerical range. It still sends me an error when I type nothing or a string in because it's trying to convert it to a double yet if I'm not doing any between range checks, it checks just fine if it's only numeric with no errors.

Can anyone help me fix this? Thanks!

4 Answers 4

1

You could use CInt to convert the string to Integer

Private Function inputValid(input As String, min As Integer, max As Integer)
    Dim v as Integer
    If Not IsNumeric(input) Then
        Return False
    End If
    v=CInt(input)
    If v < min Or v > max Then
        Return False
    Else
        Return True
    End If
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

This is the problem I'm having, I try things like this and it still doesn't work and I just don't know why. It gets the same problem. I don't understand at what point it's trying to convert to a double.
1

You might be better off served using a regular expression to do the numeric validation on the string

Private Function inputValid(input As String, min As Integer, max As Integer)
    dim regex as new Regex("[\d]+")    
    If not regex.isMatch(input) OrElse cint(input) > min OrElse cint(input) < max Then
        Return False
    Else
        Return True
    End If
End Function

1 Comment

Now you've introduced a new problem: a regular expression. Note that Regex.IsMatch("١٢٣", "[\d]+") returns True. In .NET, \d stands for \p{Nd} unless you also specify RegexOptions.ECMAScript. (Ref: Decimal Digit Character). Explicitly specifying [0-9] is safer.
1

You can use the Integer.TryParse function to check if a string can be converted to an integer.

Also, you can return a boolean which is the result of a comparison: instead of something like If x > 4 Then Return True you can use Return x > 4.

So your function could look like

Private Function StringIsValidInteger(s As String, min As Integer, max As Integer) As Boolean
    Dim tmp As Integer

    If Integer.TryParse(s, tmp) Then
        Return (tmp >= min AndAlso tmp <= max)
    End If

    Return False

End Function

Comments

0

Sorry, guys. I was returning true and false the wrong way around it appears because I needed to continue the loop it needed to be true where it would usually be false in regular validation.

By simply changing around in the inputValid function the true and false return types, it works fine.

Thanks for your help guys.

1 Comment

This shows you that you need to be carefuil when you choose the name of a function. Also, note the IsNumeric returns True for strings like "1.2", "1e2" and "&H2".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.