locked
I think I'm missing something.. RRS feed

  • Question

  • Hey Marten here again, 



    I contineud my yourney in to the world Visual Basic

    And I am now trying to create a windows application that allows you to calculate the surface or outline of a cirkle.

    The code it self works perfectly.. Insert a value and it generates a message box with the correct outcome. 

    But when I try to add a IF statement to generate a Error message box for when there's no inserted value. 

    The application litterally crashses upon no user input value.

    Here's the entire code.. 

        Public Property MessageBoxIcon As Object
        Public Property MessageBoxButtons As Object
    
        Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
    
            Const pi As Double = 3.14159265
    
            Dim r, a As Single
            r = textBox.Text
            a = (2 * pi * r)
    
    
            MsgBox(a)
    
    
    
            If textBox.Text = "" Then
                MessageBox.Show("Error: Please insert proper value",
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
                With textBox1
    
                    .Focus()
                    .SelectAll()
                End With
            End If
    
    
    
    
    
        End Sub
    
        Private Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
    
            Const pi As Double = 3.14159265
    
            Dim r, a As Single
            r = textBox1.Text
            a = (r * r * pi)
    
            MsgBox(a)
    
            If textBox1.Text = "" Then
                MessageBox.Show("Error: Please insert proper value",
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
                With textBox1
    
                    .Focus()
                    .SelectAll()
                End With
            End If
    
    
        End Sub
    End Class

    I feel like the answer is simple.. but I can't see it.. I think I might be up for too long. 


    PS: My apologies for my lack of proper English. 

    Wednesday, November 25, 2015 6:26 AM

Answers

  • Additional information: Conversion from string "" to type 'Single' is not valid.

    That's the error you are trying to prevent.  So you need to insert your test before you attempt to access the text property. And you need to ensure that the code does not then continue on.

    https://msdn.microsoft.com/en-us/library/t2at9t47.aspx

    You should insert Option Strict On as the first line of code, then address each of the errors that it reveals.  You can't, for instance, pass a Single to the MsgBox method.


    Wednesday, November 25, 2015 7:54 AM
  • Well I fixed my problem, 

    Even though it's not entirely what I had hoped for.. 


      
         Const pi As Double = 3.14159265
    
    
            Dim r, a As Single
            r = textBox.Text
            a = (2 * pi * r)
    
    
    
            Dim c As Integer
    
            c = textBox.Text
    
    
    
            If c = 0 Then
                MsgBox("Error: Please insert proper value", MessageBoxButton.OKCancel)
    
                With textBox
    
                    .Focus()
                    .SelectAll()
                End With
    
            Else
                MsgBox(a)
            End If
    
    
        End Sub
    
        Private Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
    
            Const pi As Double = 3.14159265
    
            Dim r, a As Single
    
            r = textBox1.Text
            a = (r * r * pi)
    
            Dim c As Integer
    
            c = textBox.Text
    
    
    
            If c = 0 Then
                MsgBox("Error: Please insert proper value", MessageBoxButton.OKCancel)
    
                With textBox
    
                    .Focus()
                    .SelectAll()
                End With
            Else
                MsgBox(a)
            End If
    
        End Sub

    I should really look more in to TryParse.. 

    Thanks for the help!

    Wednesday, November 25, 2015 8:49 AM
  • Well I fixed my problem, 

    Even though it's not entirely what I had hoped for..

    TryParse will do both jobs - checking for an empty string and also checking that the input can be converted.  But you still haven't fixed the underlying problem - you are still testing the value after you use it in the calculation.  You need to test it before you use it (whatever test you use) and you need to ensure that the code does not continue on to do the calculation with an invalid value.

    You should insert Option Strict On as the first line of code, then address each of the errors that it reveals.  You can't, for instance, pass a Single to the MsgBox method.

    Wednesday, November 25, 2015 10:23 AM

All replies

  • But when I try to add a IF statement to generate a Error message box for when there's no inserted value.

    What is the details of the error - the message, the line of code, the value of relevant variables?  That usually indicates what the problem is.

    If it is the contents of the text property of a textbox that you want to check, then the test should occur before you use the value.

    You should insert Option Strict On as the first line of code, then address each of the errors that it reveals.  You can't, for instance, pass a Single to the MsgBox method.

    I would not recommend using a variable name 'textbox' that is also a class name - generally the compiler understands the difference, but it can create confusion. Or is that a typo?

    Wednesday, November 25, 2015 6:36 AM
  • the error states: 



    Additional information: Conversion from string "" to type 'Single' is not valid. 


    And I think textbox was a typo, 

    So in short.. I can't register a " "  value from a single in to a error message?? 
    Wednesday, November 25, 2015 7:02 AM
  • Additional information: Conversion from string "" to type 'Single' is not valid.

    That's the error you are trying to prevent.  So you need to insert your test before you attempt to access the text property. And you need to ensure that the code does not then continue on.

    https://msdn.microsoft.com/en-us/library/t2at9t47.aspx

    You should insert Option Strict On as the first line of code, then address each of the errors that it reveals.  You can't, for instance, pass a Single to the MsgBox method.


    Wednesday, November 25, 2015 7:54 AM
  • Well I fixed my problem, 

    Even though it's not entirely what I had hoped for.. 


      
         Const pi As Double = 3.14159265
    
    
            Dim r, a As Single
            r = textBox.Text
            a = (2 * pi * r)
    
    
    
            Dim c As Integer
    
            c = textBox.Text
    
    
    
            If c = 0 Then
                MsgBox("Error: Please insert proper value", MessageBoxButton.OKCancel)
    
                With textBox
    
                    .Focus()
                    .SelectAll()
                End With
    
            Else
                MsgBox(a)
            End If
    
    
        End Sub
    
        Private Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
    
            Const pi As Double = 3.14159265
    
            Dim r, a As Single
    
            r = textBox1.Text
            a = (r * r * pi)
    
            Dim c As Integer
    
            c = textBox.Text
    
    
    
            If c = 0 Then
                MsgBox("Error: Please insert proper value", MessageBoxButton.OKCancel)
    
                With textBox
    
                    .Focus()
                    .SelectAll()
                End With
            Else
                MsgBox(a)
            End If
    
        End Sub

    I should really look more in to TryParse.. 

    Thanks for the help!

    Wednesday, November 25, 2015 8:49 AM
  • Well I fixed my problem, 

    Even though it's not entirely what I had hoped for..

    TryParse will do both jobs - checking for an empty string and also checking that the input can be converted.  But you still haven't fixed the underlying problem - you are still testing the value after you use it in the calculation.  You need to test it before you use it (whatever test you use) and you need to ensure that the code does not continue on to do the calculation with an invalid value.

    You should insert Option Strict On as the first line of code, then address each of the errors that it reveals.  You can't, for instance, pass a Single to the MsgBox method.

    Wednesday, November 25, 2015 10:23 AM