Karrthus
Karrthus

Reputation: 21

Check for input being an integer

I am aware that there are quite a few questions around on here regarding this, but none of the answers seem to work for me. I'm trying to make my program check to see if the given input is a 1 or a 0 and i really have hit a dead end. I'm still new to programming and the internet has helped load when it comes to this but i cant find anything

this program is a thingy I'm doing for my coursework, the bitty im trying to get this on is as follows

Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Sensor1, Sensor2, Sensor3, SensorTotal, Temp As Integer

    Sensor1 = InputBox("Please enter the state of Sensor 1, 0 for off, 1 for on", "System")
    If Sensor1 = 1 Then PictureBox5.Hide()

    Sensor2 = InputBox("Please enter the state of Sensor 2, 0 for off, 1 for on", "System")
    If Sensor2 = 1 Then PictureBox6.Hide()

    Sensor3 = InputBox("Please enter the state of Sensor 3, 0 for off, 1 for on", "System")
    If Sensor3 = 1 Then PictureBox7.Hide()

I need to detect if the input from each sensor is being given as a 1 or a 0 from the input box. a dead end has been hit and i cannot progress any further on this program until i work out/ find a way yo get this done.

It needs to check if the given value is a 1 or a 0 given as an integer, if its not a 1 or a 0 it needs to ask again and keep asking until a 1 or a 0 is given

Upvotes: 1

Views: 80

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27322

You need to use Integer.TryParse - it is probably best to put this all in a separate function:

Public Function GetInputState(sensorNum as Integer) As Integer
    Dim inputValue as Integer = -1
    Do 
        Dim inputString = InputBox("Please enter the state of Sensor " & sensorNum &  ", 0 for off, 1 for on", "System")
        If Not Int32.TryParse(inputString, inputValue) then inputValue = -1
    Loop Until inputValue = 1 OrElse InputValue = 0
    Return inputValue
End Function

Int32.TryParse return True if the Parse succeeded, so if the parse fails it sets the value to -1 to ensure that it goes round the loop again and asks for a number. If the parse succeeds it checks the value is either zero or 1.

Then you can be sure that the value entered is 1 or 0 like this:

Sensor1 = GetInputState(1)
Sensor2 = GetInputState(2)
Sensor3 = GetInputState(3)

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460048

You can use Int32.TryParse and following loops:

Dim Sensor1, Sensor2, Sensor3, SensorTotal, Temp As Integer
Dim sensor1Valid, sensor2Valid, sensor3Valid As Boolean
Dim validSensorValues = {0, 1}

While Not sensor1Valid
    Dim s1 As String = InputBox("Please enter the state of Sensor 1, 0 for off, 1 for on", "System")
    sensor1Valid = Int32.TryParse(s1, Sensor1) AndAlso validSensorValues.Contains(Sensor1)
End While
If Sensor1 = 1 Then PictureBox5.Hide()

While Not sensor2Valid
    Dim s2 As String = InputBox("Please enter the state of Sensor 2, 0 for off, 1 for on", "System")
    sensor2Valid = Int32.TryParse(s2, Sensor2) AndAlso validSensorValues.Contains(Sensor2)
End While
If Sensor2 = 1 Then PictureBox6.Hide()

While Not sensor3Valid
    Dim s3 As String = InputBox("Please enter the state of Sensor 3, 0 for off, 1 for on", "System")
    sensor3Valid = Int32.TryParse(s3, Sensor3) AndAlso validSensorValues.Contains(Sensor3)
End While
If Sensor3 = 1 Then PictureBox7.Hide()

Upvotes: 1

Related Questions