Jump to content

VB.NET from beginner to advanced programmer Part 14 - Difference between Scopes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.


VB.NET from beginner to advanced programmer





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.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Reading this, I never really understand what a private, friend or public variable is - just how to use them (I'm reading from a beginner point of view, of course I already know what they are). Perhaps some explanation is needed?

Otherwise, nice job. +rep!

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I thought I made it clear but maybe not. I'll see what I can do.

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I think you also typed the Friend Variable as public:
 Public FriendValue As Integer = 2
:)