+ Reply to Thread
Results 1 to 4 of 4

Thread: VB.NET from beginner to advanced programmer Part 18 - Classes

  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 18 - Classes

    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





    Much information awaits you in this part too, don't be afraid, it's only these two parts that is this long, current subject is Classes. Classes are also a container type just as structures and are in many ways the same as structures. Since this is the case I will in this part focus on the difference of them rather then explaining what a class is even though I will have the classes as the main topic in the comparing.


    A very important difference is that structures are value types and classes are reference types. This means that structures only stores values of all its members while classes stores references to the values. You'll soon understands what this means.

    First example is when creating new instances, we have one simple class and one simple structure with only one variable each:


    Code:
    Structure theStructure
        Public theVariable As Integer
    End Structure
    
    Class theClass
        Public theVariable As Integer
    End Class


    Structures:

    We're accessing the variable theVariable and change its calue to 10, this goes fine and no errors occurs:
    Code:
            Dim myInstance As theStructure
            myInstance.theVariable = 10
    Classes:

    But when doing the same thing with the class:

    But when doing the same thing with the class:
    Code:
            Dim myInstance As theClass
            myInstance.theVariable = 10
    we'll receive this error:

    Code:
    NullReferenceException was unhandled
    The reason we got this is because the variable theVariable is not a part of Nothing. But we did create an instance of theClass so why does myInstance contains nothing? The reason is that since Classes are a reference type we haven't actually created a new instance of theClass, we have only created a variable of the type theClass but it's still empty. To give it a value we can do it like this:

    Code:
            Dim myInstance As theClass = New theClass
    Or even simpler:

    Code:
            Dim myInstance As New theClass

    This leads as to a function we can do in a class, we can create a sub that is called New which the will be executed when a new instance is crated, an example on this:

    Code:
    Class theClass
        Private theVariable As Integer
    
        Public Sub New(ByVal value As Integer)
            theVariable = value
        End Sub
    
    End Class
    So in that example we accepts a parameter called value which we'll store in theVariable when the class instance is created. If we don't have any parameters you just creates a new instance the same way as before and if you have it's just to set some values for the required parameters, like so:

    Code:
            Dim myInstance As New theClass(42)



    Another example is when storing a structure or a class in different variables, for this examples I use the same class and structure as in the last example:


    Code:
    Structure theStructure
        Public theVariable As Integer
    End Structure
    
    Class theClass
        Public theVariable As Integer
    End Class

    Structure:


    Code:
            Dim myInstance As theStructure
            myInstance.theVariable = 20
            Dim myCopy As theStructure = myInstance
            myCopy.theVariable = 5
            MessageBox.Show("myInstance.theVariable = " & myInstance.theVariable & " and myCopy.theVariable = " & myCopy.theVariable)
    We creates an instance of the structure and change the value of theVariable to 20. Then we creates a copy of the instance which will copying all the values over to the new one. And then we change theVariable to 5 in the new one. But since structures are value types myInstance.theVariable is still 20 which gives us the output: "myInstance.theVariable = 20 and myCopy.theVariable = 5".



    Classes:

    Code:
            Dim myInstance As New theClass
            myInstance.theVariable = 20
            Dim myCopy As theClass = myInstance
            myCopy.theVariable = 5
            MessageBox.Show("myInstance.theVariable = " & myInstance.theVariable & " and myCopy.theVariable = " & myCopy.theVariable)

    Now we're doing the same thing with classes instead. But since classes are reference types they just hold references to the actual values which means that myInstance and myCopy will be the same instance of the class, therefor when we change theVariable of myCopy we actually change the same instance as we did before and therefor the output of this code will be:
    "myInstance.theVariable = 5 and myCopy.theVariable = 5".




    Inherits


    A thing classes can do which structures can't is that we can use Inherits with classes. When we're doing this we're creating a class with another one as the base, it will then inherit everything from the base class. A simple example:

    Code:
    Public Class frmMain
    
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myInstance As New childClass
            MessageBox.Show(myInstance.theVariable)
        End Sub
    
    End Class
    
    
    Class childClass
        Inherits baseClass
    End Class
    
    Class baseClass
        Public theVariable As Integer = 10
    End Class
    The class childClass will have a public variable named theVariable with the value 10 since childClass Inherits from baseClass which has it.

    This maybe doesn't seem so useful but it's perfect when you want two or more classes who are pretty much the same but not exactly. Then you can do a base class and two classes which inherits from the base one.


    Put now we're coming to a problem with the scopes we're currently using. If we don't want a variable(or something else) to be accessed from without the classes, we can't use public. But if we use private, we can't access it from the class which inherits the base class, so therefor we have to use Protected. Check out this example:



    Code:
    Public Class frmMain
    
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myInstance As New childClass
            MessageBox.Show(myInstance.mySub)
        End Sub
    
    End Class
    
    
    Class childClass
        Inherits baseClass
    
        Public Function mySub()
            Return theVariable
        End Function
    End Class
    
    Class baseClass
        Protected theVariable As Integer = 10
    End Class

    So in this example theVariable has the scope protected, so when the base class is inherited into childClass it can still access it in mySub.



    If you want to make a class which must inherits (i.e. it is a base class only) or want to forbid inherits from that class you can also do so:

    Must Inherit:

    Code:
    MustInherit Class exampleClass
    
    End Class
    Not Inheritable:

    Code:
    NotInheritable Class exampleClass
    
    End Class


    For a complete reference of the difference you can check here even though they kinda hard to understand. That was all for this part. Let's meet in next part.
    Last edited by Vswe; 03-21-2010 at 03:37 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 18 - Classes

    Why in the world would I ever want to use a class?
    +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 18 - Classes

    What do you mean?

  5. #4
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 18 - Classes

    lol, that is the most often asked question by beginners. They don't see a point in classes.

+ 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, 06:03 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 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