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
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:
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:
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:
Dim myInstance As theClass
myInstance.theVariable = 10
we'll receive this error:
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:
Dim myInstance As theClass = New theClass
Or even simpler:
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:
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:
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:
Structure theStructure
Public theVariable As Integer
End Structure
Class theClass
Public theVariable As Integer
End Class
Structure:
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:
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:
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:
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:
MustInherit Class exampleClass End Class
Not Inheritable:
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.
Edited by Vswe, 21 March 2010 - 02:37 PM.


Sign In
Create Account


Back to top









