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
- 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:
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:
Classes:Code:Dim myInstance As theStructure myInstance.theVariable = 10
But when doing the same thing with the class:
But when doing the same thing with the class:
we'll receive this error:Code:Dim myInstance As theClass myInstance.theVariable = 10
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:NullReferenceException was unhandled
Or even simpler:Code:Dim myInstance As theClass = New theClass
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:
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:Class theClass Private theVariable As Integer Public Sub New(ByVal value As Integer) theVariable = value End Sub End Class
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:
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".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)
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:
The class childClass will have a public variable named theVariable with the value 10 since childClass Inherits from baseClass which has it.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
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:
Not Inheritable:Code:MustInherit Class exampleClass End Class
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.
Why in the world would I ever want to use a class?
+rep
What do you mean?
lol, that is the most often asked question by beginners. They don't see a point in classes.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks