Page 1 of 1

Classes, very simply explained A very short introduction into classes Rate Topic: ***** 2 Votes

#1 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Posted 30 March 2010 - 06:24 AM

*
POPULAR

Hi,

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...

Is This A Good Question/Topic? 6
  • +

Replies To: Classes, very simply explained

#2 williamgeorgegardner  Icon User is offline

  • CEO of GeekTelligence
  • member icon

Reputation: 19
  • View blog
  • Posts: 584
  • Joined: 27-December 09

Posted 31 March 2010 - 12:15 PM

Very nice tutorial you should make more as I am using them to learn VB.NET.

This post has been edited by comptechexpert: 31 March 2010 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

Posted 14 April 2010 - 02:14 PM

From your code could you describe what would happen if you clicked the button twice? Would you create two identical but distinguishable instances of the class or would you just write over the intial? Is this getting too far into the object side of things?

Drew
Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Posted 21 April 2010 - 11:46 AM

Well in this case it creates a new instance, when it hits 'End Sub' the instance is destroyed.

To make it visible at class level you can do this



Private BeerMug as New BeerMug() 

Private Sub btnCreateBeerMug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBeerMug.Click


    Dim Litres As Double
    
    'access the functions from "BeerMug" here...
    Litres = BeerMug.ConvertGallonsToLitre(1)
End Sub


This post has been edited by motcom: 23 April 2010 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 IndyLateNite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-January 10

Posted 25 May 2010 - 12:38 PM

Thanks for the tutuorial. It has cleared up some misconceptions I had reguarding classes. Pretty new to vb.net,
Was This Post Helpful? 0
  • +
  • -

#6 MistGun  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 10

Posted 05 November 2010 - 07:12 AM

Thanks for this tutorial man.
I hope you write another one about modules too!
Was This Post Helpful? 0
  • +
  • -

#7 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Posted 10 November 2010 - 12:51 AM

Modules are a bit different, they do not behave like Classes do. All the methods and variables in a module are visible throughout your project. It's a dangerous way of doing things. If you do not absolutely make sure where and how you use variables placed in modules, it could have dire consequences as they can change anywhere in your project. I'm not saying you should not use it, but if you do, be very very carefull.
Was This Post Helpful? 1
  • +
  • -

#8 MistGun  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 10

Posted 13 November 2010 - 01:14 PM

View Postmotcom, on 10 November 2010 - 07:51 AM, said:

Modules are a bit different, they do not behave like Classes do. All the methods and variables in a module are visible throughout your project. It's a dangerous way of doing things. If you do not absolutely make sure where and how you use variables placed in modules, it could have dire consequences as they can change anywhere in your project. I'm not saying you should not use it, but if you do, be very very carefull.


Thanks for your useful post bro.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1