VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
One thing I meantioned as early in party two is that there exists something that's called a sub and an Event block is one. A sub is a block of code where most of all codes has to be in(not variable declarations, classes and structures, for example). A sub must then be called in some way, by another sub/function(function is a sort of sub but we comes to that later on) or by an object through an event. This means everything has to begin with an event in one way or another.
Here below I show a simply example on how to create and use your own sub. one of the subs is called by the event block which is called when a button is clicked:
Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
ExampleSub()
ExampleSub()
End Sub
Private Sub ExampleSub()
MessageBox.Show("This is inside ExampleSub.")
End Sub
When we click the button the program will start running at the beginning of cmdExample_Click which is the event block for cmdExample.click. From here we call ExampleSub by just writing its name and two parentheses("ExampleSub()"). Now the program will continue at the beginning of ExampleSub, here a messagebox will be shown. When the program has reached the end of ExampleSub it goes back where it was before. Since we have wrote "ExampleSub()" twice, the sub is called again. This is what you use subs and functions for, you can call the sub from anywhere instead of writing the same code everywhere.
Using parameters:
Parameters are like variables to a sub that you set each time you call the sub. A parameter can be wither ByVal or ByRef but more to that soon. If you want you can also set the parameter to optional, this means what it sounds like, you don't have to set its value. But to the right of an optional parameter you can't have a non-optional parameter. We modify they code we had before to include some parameters. Here's the example on how to use parameters:
Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
ExampleSub(3, 2, 5)
ExampleSub(3, 5)
End Sub
Private Sub ExampleSub(ByVal X As Integer, ByVal Y As Integer, Optional ByVal Z As Integer = 1)
If Z <> 1 Then
MessageBox.Show("X*Y*Z = " & X * Y * Z)
Else
MessageBox.Show("X*Y = " & X * Y)
End If
End Sub
We have now added 3 Integer parameters to ExampleSub. Z is optional and therefor we have to give it an default value, Z will get this value if we aren't using it when calling the sub. Except where and how to declare them parameters work as variables when they are ByVal. When they are ByRef there will be another difference, but more to that later. Inside ExampleSub we're using an if statement to check if Z has its default value. If it has we show the product of only X and Y but if it has another value we shows the product of X, Y and Z. Then in the our event block for the button we first call ExampleSub(3, 2, 5). This will run ExampleSub with X=3, Y=2 and Z=5. Since Z doesn't have its default value the program will use that too in the calculation and we will see: "X*Y*Z = 30". Then we'll call ExampleSub again, this time with X=3 and Y=5. Since we doesn't set the value of Z, it will keep its default value. Since it has its default value the program will show us: "X*Y = 15"
ByVal and ByRef:
When declaring a parameter you can choose to declare it By Value(ByVal) or
By Reference(ByRef). ByVal will only copy the value and use it without changing the original value. ByRef however will not copy the value but instead get a "link" to the original value and when we change the value of our parameter declared by reference it's actually only linking to the original value so that is what we change. Of course I will show you an example on how this works:
ByVal:
Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
Dim nOriginalValue As Integer = 10
ExampleSub(nOriginalValue)
MessageBox.Show(nOriginalValue)
End Sub
Private Sub ExampleSub(ByVal nParameterValue As Integer)
MessageBox.Show(nParameterValue)
nParameterValue = 5
End Sub
ByRef:
Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
Dim nOriginalValue As Integer = 10
ExampleSub(nOriginalValue)
MessageBox.Show(nOriginalValue)
End Sub
Private Sub ExampleSub(ByRef nParameterValue As Integer)
MessageBox.Show(nParameterValue)
nParameterValue = 5
End Sub
The only difference in the above codes are which one of ByVal and ByRef the parameter is declared as. In both we first declare an Integer variable with the value 10. Then we call ExampleSub with nParameterValue=nOriginalValue. A messagebox is then showed with the value of the parameter and then we change the parameter's value to 5. Then when ExampleSub has ended we show the value of nOriginalValue in a messagebox.
In the ByVal example, we'll first see "10". The change will not affect the original value so we'll see "10" again.
In the ByRef example, we'll first see "10". The change of the parameter will here change the original value so we'll see "5" in the second messagebox.
Functions:
By replacing sub with function when we create a sub we gets a function instead. The difference is that a Function may and must return a value. To do this we use the functions name as a variable to give it values. We can also use "Return <value>" where value is the value we want the function to return. When using the return statement the function will stop right away to return the value, if we just set the return value as a variable that value is returned when the function ends the normal way: Here comes an example on how to declare and use a function:
Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
MessageBox.Show(ExampleFunction(5, 4))
End Sub
Private Function ExampleFunction(ByVal X As Integer, ByVal Y As Integer) As Integer
Return X + Y
End Function
In the example above the value of "ExampleFunction(5, 4)" will be showed in a messagebox. The value of "ExampleFunction(5, 4)" is the value which will be returned from the function when calling it with X=5 and Y=4. The function will simply return the sum of X and Y. So the output of this code will be 9.
Now when you have learned about sub and functions we can continue with scopes.
Edited by Vswe, 21 March 2010 - 02:34 PM.


Sign In
Create Account


Back to top









