+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

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

    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
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. 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..."

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


    Now a window will pop up, select the type and name and then press add.

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

    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:


    Code:
    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:
    Code:
        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:

    Code:
    Messagebox.Show(PublicValue + FriendValue + PrivateValue)
    We will now see the error:

    Code:
    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:

    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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.
    Last edited by Vswe; 03-21-2010 at 03:34 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

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

    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!

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

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

    I thought I made it clear but maybe not. I'll see what I can do.

  5. #4
    Jordan Guest

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

    I think you also typed the Friend Variable as public:
    Code:
     Public FriendValue As Integer = 2

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 05:37 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. VB.NET from beginner to advanced programmer Part 10 - Arrays
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-02-2009, 05:45 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts