Putting a function into another function

I can't find a clear answer to this problem. What am I doing wrong

I am having problems writing this piece of code. it is supposed to be a customized function that has within it another function. It is supposed to see if content is Non Numeric. If it is non numeric, a messagebox is displayed, if it is not the message box is not activated, and the software continues to the next argument.

Here is a sample of the code:

Function TextIsNotNumeric(ByVal txtBox As TextBox) As String

If Not IsNumeric(txtBox) Then

MessageBox.Show("input is Not Numeric - Re enter")

End If

End Function

'and this part is where I call the code in the main program

Private Sub btnAddTOState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTOState.Click

TextIsNotNumeric(txtVotes)

End Sub



Answer this question

Putting a function into another function

  • Skipping Rock

    If Not IsNumeric(txtBox.text) Then

  • Nikita Mironov

    It helps if you tell us what error messages you are getting. Here is a quick form I wrote:

    Public Class Form1

    Sub TextIsNotNumeric(ByVal txtBox As TextBox)

    If Not IsNumeric(txtBox.Text) Then

    MessageBox.Show("input is Not Numeric - Re enter")

    End If

    End Sub

    Private Sub btnAddTOState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTOState.Click

    TextIsNotNumeric(txtVotes)

    End Sub

    End Class


  • bbaldwinwa

    You cannot create a function within a function. This is not a valid action.

    You can call a function from with a function - this is a perfectly valid action.

    If your method does not need to return any values then you can use a Sub

    Sub Bar
    msgbox("hello")
    End Sub

    However, If you want to return a value from you method then on you method you would use function similar to the following

    Function foo() as boolean
    '<You Code>
    Return True
    End Function

    This has a boolean return type and is set to return a value of true, of course you can use different types other than boolean.


  • JDELUNA

    My intention was to create a function inside of a function and to return a value for the True and the false. However, after I plugged this thing in as a private sub, in this particular case, it really made not difference.

    So to return a value in a function would it be as simple as just putting the word "return" at the end of the function argument to get the little green line to disappear


  • snowrabbit

    You are right In this particular case the program did not care if it came from a private sub or a function.

    I can plug it into a program like a function that returns a value. Unless I were trying to add up the trues and falses, does it make any difference in this case is there any advantage to using the function feature in this case


  • Shajeel

    When not returning a value from your function you should use a Sub instead

    The little green line is telling you that the function does not return a value on all paths.....if it is your intention only to show a message box then use the sub....if you are wanting to reurn a value ie (true or false) whether or not the textbox value is numeric then you need to use a function and give it a return value



  • joynerCN

    Now the argument works almost perfectly, except there is a little green line underneath the

    End Function (This example does not have the green line underneath it. )

    Otherwise the added term .text fixed most everything.

    If I run into other problems I will be sure and send out another SOS. Thanks much!


  • Putting a function into another function