A function is more or less the same thing as a Sub - a segment of code you create yourself, and that can be used whenever you want it. The difference is that a Function returns a value, while a Sub doesn't. When you Called a Sub you did this:
Call AddNumbers( first, second )
Visual Basic will go off and execute that code for you, and then drop down to the next line. The SubAddNumbers is not a value, it's not equal to anything. It's not like a normal variable where you assign something to it. It's just the name of your Subroutine.
A Function is different. It is a value, will be equal to something, and you do have to assign a value to it. You create a Function in the same way you did a Sub, but this time your code will be like this:
Private Function ErrorCheck () As Boolean
End Function
First, we've changed the word "Sub" to "Function"; second we've added "As" something, in this case "As Boolean". The name we called our Function is ErrorCheck, and ErrorCheck is now just like a variable. And just like a variable, we use one of the Types. We can use "As Integer", "As Long", "As Double", "As String", or any of the variable types.
Let's write some code, and try an example.
Add a new button and a textbox to your form. Change the Name of the textbox to txtFunction. Double click your button and add the following code to it (add it after the End Sub of the button, but before theEnd Class):
Private Function ErrorCheck () As Boolean
Dim TextBoxData As String
TextBoxData = Trim(txtFunction.Text)
If TextBoxData = "" Then
MsgBox("Blank Text Box detected")
ErrorCheck = True
End If
End Function
This is almost the same code from our Sub called ErrorCheck, in a previous section. The difference is the one added line - ErrorCheck = True. Remember that ErrorCheck is now like a variable. In this case it was a Boolean variable. So if there's nothing in the Textbox, we have set ErrorCheck to True.
An Else part can also be added to The IF Statement to set ErrorCheck to False, otherwise you may get a green underline in the code editor for End Function :
Else
ErrorCheck = False
End If
Again, this code is not doing much good by itself. We need a way to use it. This time, because we've set up a Function, we have to assign the value of the function to a variable. Like this:
Dim IsError As Boolean
IsError = ErrorCheck()
Here, we are saying "Run the function called ErrorCheck. When you have finished, assign the value of ErrorCheck to the variable called IsError".
Once that code is executed we can then use the variable IsError and test its value. If it's true, then we know that the user did not enter anything into the Textbox; if it's False, then we know that they did. The benefit of using a Function to check for our errors is that we can halt the programme if IsError = True. Like this:
If IsError = True then
Exit Sub
End If
So double click your button and add the following:
Dim IsError As Boolean
IsError = ErrorCheck()
If IsError = True then
Exit Sub
Else
MsgBox("IsError = False")
End If
Run your programme again. Click the button when the textbox is blank, and see what happens. Then enter some text into the textbox, and click your button again.
To sum up, then. A function will return a value. You put this value into the name of your Function. You then assign the value of the Function to a variable. You can then test the variable to see what's in it.
No comments:
Post a Comment