DNelson
DNelson

Reputation: 7

Passing arguments to methods in VB

I'm hoping you guys can help with a problem that should be simple to solve, I've just had issues finding a solution. In the program that I'm writing some of the textbox's have to be numeric between 1 and 10, and others just have to be numeric. Instead of coding each textbox to verify these parameters I decided to write methods for each of them. I'm having problems passing the arguments and getting it to function correctly. Included is some of my code that shows what I'm trying to accomplish.

 Public Shared Sub checkforonetoten(ByVal onetoten As Double)
    If (onetoten > 1 & onetoten < 10) Then
    Else
        MessageBox.Show("Please enter a Number between 1-10", "Error")
    End If
End Sub

Public Shared Sub checkfornumber(numCheck As Double)
    Dim numericCheck As Boolean
    numericCheck = IsNumeric(numCheck)

    If (numericCheck = False) Then
        MessageBox.Show("Please enter a number", "Error")
    End If
End Sub

Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
    Dim S As Double
    S = textboxS.Text
    checkfornumber(S)
    checkforonetoten(S)
End Sub

Upvotes: 0

Views: 164

Answers (2)

one way to structure it is to have one event procedure process the similar TB types:

Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
    Handles textbox1.TextChanged, textbox12.TextChanged, _
    Handles textbox16.TextChanged

    Dim S As Double
    If Double.TryParse(Ctype(sender, TextBox).Text, S) Then
        ' or place the Check code here for all the TextBoxes listed above

        checkforonetoten(S)

   End If
End Sub

The plain numeric kind:

Private Sub textboxQ_TextChanged(sender As Object, e As EventArgs) _
    Handles textbox2.TextChanged, textbox6.TextChanged

    Dim S As Double
    If Double.TryParse(Ctype(sender, TextBox).Text, S) = False Then

       MessageBox.Show("Please enter a number", "Error")

   End If
End Sub

Rather than calling a function and passing the current TextBox from events (which is fine), have 2 or 3 events process them all. The point is adding/moving the Handles clause to a common event procedure (be sure to delete the old ones).

If you do decide to call a common function, dont do anything in the events (you still have one per TB) and do it all in the common proc:

  Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
    Handles textboxS.TextChanged

     checkforonetoten(Sender)
  End Sub

  private Sub checkforonetoten(tb As Textbox)

    Dim S As Double
    If Double.TryParse(tb.Text, S) Then

       ' your check for 1 - 10 on var S
    else
       ' error: not a valid number

    End If
  end sub

Also:

 If (onetoten > 1 & onetoten < 10) Then

should be:

 If (onetoten > 1) AndAlso (onetoten < 10) Then

Upvotes: 0

tinstaafl
tinstaafl

Reputation: 6958

One of your main problems is you're converting your text without validating it. You're also programming without the Options On to warn you of bad conversion techniques like you're using in the event handler.

The TryParse method would come in handy here:

Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
    Dim S As Double
    If Double.TryParse(textboxS.Text, S) Then
        checkforonetoten(S)
    End If
End Sub

Since the TryParse method validates your text and sets the value to 'S', you only need to check the range.

Of course using NumericUpDown controls would make all this moot, since the values will always only be numbers and you can set the range on each one.

Upvotes: 1

Related Questions