VB.NET Object-oriented programming

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
2 posts Page 1 of 1
Contributors
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

VB.NET Object-oriented programming
Kieken72
Hello CNS

First of all what is Object-oriented programming (OOP) ?
"Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP, at least as an option."
"Wikipedia Article OOP"

Why would we use it?
Because all the things u use e.g. Random is a class. Most people just put their code in the object or a function. When u code it oop. You will have more benifits: Cleaner code, easy to use for other people and for other people who edit your "Class".

First of all I'll start with an example. In Belgium we have a shop "Colruyt" this shop has many different goods so a program for there logistick department needs to get the information and put into a database.

First of all we need to create a class:
Image

Then a good name, mostly I start my classes with the small "c" followed by the classname for this one I'll take "cGoods".
After u made your class you will need to declare all your variables. When u've got static values (don't change) you'll use a Constant:
Code: Select all
 private Const CONST_... As ... 
Why private?
So u can't acces your variables directly.
Declared all?
In this example I don't have a constant.
Code: Select all
 
Private dblBuyPrice, dblSellPrice As Double
Private intTAXCode As Integer '0,6,21,12'
Private strProduct As String
Now if u want to acces these variables u can't. Because they are private. How we solve this? => Propery's
Code: Select all
'Get & Set '
Property BuyPrice As Double
        Get
            Return dblBuyPrice
        End Get
        Set(ByVal value As Double)
            dblBuyPrice = value
        End Set
    End Property

'Get'
ReadOnly Property ProductName as string
       Get
            Return strProduct
        End Get
    End Property

'Set'
WriteOnly Property ProductName as string
        Set(ByVal value As string)
            strProduct = value
        End Set
    End Property
Next example for the TAX Code validates if the value is able to get in. E.g. in Belgium the TAXCodes are 0,6,12 and 21.
Code: Select all
Property TAXCode As Integer
        Get
            Return intTAXCode
        End Get
        Set(ByVal value As Integer)
            Select Case value
                Case 0
                intTaxCode = value
                Case 6
                intTaxCode = value
                Case 12
                intTaxCode = value
                Case 21
                intTaxCode = value
                Case Else
                Throw New NotExistingTAXCodeException
            End Select
        End Set
    End Property
Maybe u are wondering WTF is a "NotExistingTAXCodeException" ?
I made it myself? nerner;
How:
Create a new class named: ....Exception
The only thing u write in it is:
Code: Select all
Inherits Exception
Wow you made your own exception!

I think this is it. In OOP u mostly write down what u going to make and divide it in classes who do specific functions. E.g. I made a shooting cannon in vb.net for school and I had 3 classes:
cBal: Just the ball and its formules
cCoordinates: This converts a x and y coordinate to a screencoordinate
cCorner: Calculates Degrees to radians and radians to degrees.

I hope u learned something :oops:
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: VB.NET Object-oriented programming
Shim
nice tutorial :-) . this will be really helpful to visual basic begginers ;-)
Find my programs on Softpedia
2 posts Page 1 of 1
Return to “Tutorials”