I was requested to post this under the tutorials, so here it goes...
In VB.Net, C# etc, using the DotnetFramework, everything is normally placed in a Class.
Now a Class is in most cases something related to a Noun (lets say we call it a BeerMug)
So you start of with declaring a Class like this
Public Class BeerMug End Class
Now the BeerMug has some Properties like how many Litres it can hold, how much it weighs etc...
So to add those properties to the Class, you would do this (notice the Keywords Private and Public)
Private Variables are not accessible outside the class, thus we use Public Properties to do that work
I know its confusing in the beginning, but trust me ist so much more fun when you understand it..
So here it goes...
Public Class BeerMug 'we will not bother with values like 0.5 rather be it 500 ML so keep it a Integer Private _Capacity As Integer Public Property Capacity As Integer Get 'this returns the value of the _Capacity Variable Return _Capacity End Get Set(ByVal value As String) 'this sets the value of the _Capacity Variable _Capacity = value End Set End Property End Class
Now Lets add some Functionality to this class - we want the beermug to get filled somehow so lets do that
Public Class BeerMug 'we will not bother with values like 0.5 rather be it 500 ML so keep it a Integer Private _Capacity As Integer Public Property Capacity As Integer Get 'this returns the value of the _Capacity Variable Return _Capacity End Get Set(ByVal value As String) 'this sets the value of the _Capacity Variable _Capacity = value End Set End Property Public Sub FillMug(ByVal HowManyMilliLitres As Integer) 'now we can do this (setting the variable directly) _Capacity = HowManyMilliLitres 'or we can do this, both have the same affect (using the Property to set the variable) 'Capacity = HowManyMilliLitres End Sub End Class
You should notice that each class has a Constructor and a Destructor - this means every class has a
Sub New()
You can overload the sub - meaning making anohter sub new() and passing variables like this
Public Class BeerMug Public Sub New() 'If the Class inherits a Form then normally InitializeComponent() will be Called 'InitializeComponent() 'but since we do not use a windows form in this case, we will ignore it... End Sub 'Lets Overload the Sub And Pass It parameter notice its still called New() but the compiler 'can differentiate between the 2 by the amount of parameters and the type of parameters passed. Public Sub New(ByVal HowManyMilliLitres As Integer) 'we will fill the BeerMug by using the Overloaded Sub New 'now we can do this (setting the variable directly) _Capacity = HowManyMilliLitres 'or we can do this, both have the same affect (using the Property to set the variable) 'Capacity = HowManyMilliLitres End Sub 'we will not bother with values like 0.5 rather be it 500 ML so keep it a Integer Private _Capacity As Integer Public Property Capacity As Integer Get 'this returns the value of the _Capacity Variable Return _Capacity End Get Set(ByVal value As String) 'this sets the value of the _Capacity Variable _Capacity = value End Set End Property Public Sub FillMug(ByVal HowManyMilliLitres As Integer) 'now we can do this (setting the variable directly) _Capacity = HowManyMilliLitres 'or we can do this, both have the same affect (using the Property to set the variable) 'Capacity = HowManyMilliLitres End Sub End Class
a destuctor normall realeases/destroys any objects that were used in the class, I will not explain this one here.. But lets see how we can use the new class... Lets say you have a Button On a Form and you want to create a new BeerMug...This is how you would do it - note we are not using the overloaded Function yet.
Private Sub btnCreateBeerMug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBeerMug.Click Dim MyNewBeerMug as New BeerMug 'lets fill the Beermug MyNewBeerMug.Capacity = 500 End Sub
This is how you use the overloaded function
Private Sub btnCreateBeerMug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBeerMug.Click Dim MyNewBeerMug as New BeerMug(500) End Sub
Now what about the sub "FillMug"??
We could do this->
Private Sub btnCreateBeerMug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBeerMug.Click Dim MyNewBeerMug as New BeerMug() MyNewBeerMug.FillMug(500) End Sub
as you can see it's all about hiding your Variables that describe the Class (at least in most cases)
and by using the properties of a class you will see that you can reuse code quite often..
There is another thing you can do with classes and that is Making Sub's/Functions "Shared", if you do this, the Shared Function can not access the Class Level Variables. In this case (what I call it) the Functions become "Helper Functions" they don't do much in respect of the Actual "BeerMug" but rather can be called to do let's say calculate how many gallons are in a Litre etc.
The Shared Function can be called without using the "New" keywoard, meaning you do not have to "Instantiate" the class....
Let's see how that would look like
First add a Shared Function to the "BeerMug" class...
Public Class BeerMug Public Sub New() 'If the Class inherits a Form then normally InitializeComponent() will be Called 'InitializeComponent() 'but since we do not use a windows form in this case, we will ignore it... End Sub 'Lets Overload the Sub And Pass It parameter notice its still called New() but the compiler 'can differentiate between the 2 by the amount of parameters and the type of parameters passed. Public Sub New(ByVal HowManyMilliLitres As Integer) 'we will fill the BeerMug by using the Overloaded Sub New 'now we can do this (setting the variable directly) _Capacity = HowManyMilliLitres 'or we can do this, both have the same affect (using the Property to set the variable) 'Capacity = HowManyMilliLitres End Sub 'we will not bother with values like 0.5 rather be it 500 ML so keep it a Integer Private _Capacity As Integer Public Property Capacity As Integer Get 'this returns the value of the _Capacity Variable Return _Capacity End Get Set(ByVal value As String) 'this sets the value of the _Capacity Variable _Capacity = value End Set End Property Public Sub FillMug(ByVal HowManyMilliLitres As Integer) 'now we can do this (setting the variable directly) _Capacity = HowManyMilliLitres 'or we can do this, both have the same affect (using the Property to set the variable) 'Capacity = HowManyMilliLitres End Sub Public Shared Function ConvertGallonsToLitre(ByVal Gallons As Double) As Double Return Gallons * 3.7854 End Function End Class
So now you can access the ConvertGallonsToLitre Function like this
Private Sub btnCalculateLitres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateLitres.Click Dim Litres As Double Litres = BeerMug.ConvertGallonsToLitre(1) End Sub
As you can see there was no need for this code
Private Sub btnCreateBeerMug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBeerMug.Click 'not needed but will work as well Dim MyNewBeerMug as New BeerMug() Dim Litres As Double Litres = BeerMug.ConvertGallonsToLitre(1) End Sub
Thats it for now, hope this can help in understanding Classes and BeerMugs...