1

I have been working on a project for my college where i have to create a program that can be used on a hand held computer for waiters. I have created another form where the user can enter a table number, however when the text box is empty after being inputted into an error occurs.

System.InvalidCastException 'Conversion from string "" to type 'double' is not valid'

Public Module GlobalVariables
    Public TableNumber As Integer
End Module

Public Class Form1


    Private Sub TableNumber_TableNumber_TextChanged(sender As Object, e As EventArgs) Handles TableNumber_TableNumber.TextChanged
        If TableNumber_TableNumber.Text > 25 Then
            TableNumber_Error.Visible = True
            If TableNumber_TableNumber.Text < 1 Then
                TableNumber_Error.Visible = True
            End If
        End If

    End Sub
End Class

This is my first time coding in visual basic, i am confused because i have not cast anything as a double.

  • What you need to do is set [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) and let Visual Studio show you the problems and suggest solutions. – Andrew Morton Feb 05 '20 at 12:22
  • Then decide what you want to happen when there is nothing in the `TableNumber_TableNumber` TextBox. You could use [Int32.TryParse](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse) to check if what has been entered could actually be a number, and not something like "Yes". – Andrew Morton Feb 05 '20 at 12:26
  • I have Option Strict on, it did not suggest anything when i was designing the code. –  Feb 05 '20 at 12:26
  • 1
    If Option Strict was On, it would have shown that there is something wrong with the line `If TableNumber_TableNumber.Text > 25 Then`. To make extra sure, you can type `Option Strict On` as the first line before all the code. – Andrew Morton Feb 05 '20 at 12:28
  • Of course you **don't** have Option Strict On. `TableNumber_TableNumber.Text > 25` compares a string to a numeric value, you cannot do that with Option Strict On. Also, when you handle the `TextChanged` event, you check if the Text property returns an empty/null string (e.g., `if string.IsNullOrEmpty(TableNumber_TableNumber.Text) then return` or `IsNullOrWhitespace` etc. as a minimal validation) – Jimi Feb 05 '20 at 12:35
  • The logic seems off, as well. If it passes the first check of ````> 25```` then it's impossible to then pass the check of ````< 1````. It sounds like you wanted to use an ````If ElseIf```` -- https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement – JohnPete22 Feb 05 '20 at 12:57

1 Answers1

0

I wrote a similar Sub based on the logic you are executing. I added some notes next to the code that might help.

Private Sub txtTest_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtTest.TextChanged

        ' Make sure textbox isn't empty.  Really you'd have front-end validation to handle this, but never hurts to add server-side
        ' I like to use the 'Me' keyword -- just a habit, but you don't have to use it
        If Not String.IsNullOrWhiteSpace(Me.txtTest.Text) Then

            ' 'R' is used to declare a double
            ' If you want to it be an Integer, then update the code accordingly
            Dim numberToCompare As Double = 0R

            ' trim the text to remove any leading/trailing spaces
            If Not Double.TryParse(Me.txtTest.Text.Trim(), numberToCompare) Then

                ' the conversion failed...user might have entered a letter or symbol
                ' display error message if desired, and exit the sub
                Return
            End If


            ' If we got here, the Parse succeeded
            If numberToCompare > 25.0R Then

                ' execute logic

            ElseIf numberToCompare < 1.0R Then

                ' execute logic
                ' if it passed the first logic check, then it won't execute this ElseIf
            Else

                ' none of the above logic checks were true
                ' execute logic

            End If


        Else
            ' display error message if desired
        End If

    End Sub
JohnPete22
  • 523
  • 3
  • 15