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
Get ready to continue, now with when and why to use different Scopes. Scopes are indicating from where something can be accessed, for example a variable. In this Part you'll need to create a form named frmMain and a module named mdlScopeTraining to be able to follow. Modules are like forms without the design. To create a new item(like forms and modules), right click on the name of your project in the solution explorer and then select "Add" and then click "New Item..."
[ATTACH]2263[/ATTACH]
Now a window will pop up, select the type and name and then press add.
[ATTACH]2262[/ATTACH]
Private, Friend - and Public scope:
Go to the Code View for the module and add some lines of code to make it look like this:
Module mdlScopeTraining
Public PublicValue As Integer = 1
Friend FriendValue As Integer = 2
Private PrivateValue As Integer = 3
End Module
We just declared 3 variables with different scopes. We create a variable in the form too and then create an event block which handles when the form loads.
Now the code looks like this:
Private TheValue As Integer = 5
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
To access the values from the module we only write their names just as we have had done if they were locally declared. But if our module instead have been a form named frmScopeTraining and we wanted to access the variable called PublicValue we have had to write frmScopeTraining.PublicValue. But since it's just a module this is unnecessary.
We add this code inside the sub:
Messagebox.Show(PublicValue + FriendValue + PrivateValue)
We will now see the error:
Name 'PrivateValue' is not declared.
The reason for that is that PrivateValue is declared with a Private scope and can therefor only be accessed from inside the module itself. The friend scope can be accessed from other items too and the Public scope can be accessed from everywhere.
So just remove that line again.
Two things with the same name:
Now instead add these two lines to the event:
Dim TheValue as Integer = 7 Messagebox.Show(TheValue)
Now we declare a Integer variable called TheValue. So now we have two variables called TheValue, one inside the event block and the other one outside. When referring to TheValue now we will get the value of the most local one, in this case we will get the value 7.
Scope and blocks:
When using all sort of block of codes(If statements, loops, functions etc.) we need to remember one important thing about scopes. You can't access something declared in another block unless you try to access it from a sub-block of the block where it's declared. That probably didn't make so much sense so please look at the examples below:
This doesn't work, we can't set the value of the variable from the other if statement:
If X = 1 Then
Dim myVariable As Integer
End If
If Y = 1 Then
myVariable = 5
End If
This doesn't work either, the declaration is inside a block and then we can't access it from outside:
If X = 1 Then
Dim myVariable As Integer
End If
myVariable = 5
However, this will work since the if statement is inside the block where the declaration is:
Dim myVariable As Integer
If Y = 1 Then
myVariable = 5
End If
This was everything about scope that will be covered in this tutorial series(remember that I haven't gone through all different scopes and some of the others might be mentioned in another part), see you in the next Part.
Attached Files
Edited by Vswe, 21 March 2010 - 02:34 PM.


Sign In
Create Account



Back to top









