Support Forums

Full Version: [TUT] Working With Classes : POO,Methodes,properties,X & Y
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We will Create This :
[Image: Untitled.gif]

POO = Programmation Orientée Objet.
As its name indicates it, we are going to create objects. But first of all what an object ?
Ok, To help you Understanding, i will give you and exemple Using "a Car"
If we want to control a car, we can, with our current knowledge, create functions to advance, Go Back, Left & Right, turn, slow down our car...
With the concept of object, we programmed functions which will be connected to the object "Car".
These methods will be present in the file of Class. We can also add many of the other functions or sub to our class. A function to advance our car, the other one to make it put off(move back,Turn "right & left"), we can also declare variables which will be useful only by this class.

Now let's Create our Objects Smile
We will Try To make The Car move, Add a class and name it "Car"
Let's add some controls. look at the screenshot :
[Image: 61625738.jpg]

Some Will say (how the hell im going make The "Car" move ( well i said that ), just Some locations to be defined and we're done.

Lets start Coding now Smile
Create a new Sub
This New Sub will get back the information crossed in argument and will make what he wants.
Now declare 2 private variables :

Code:
Private Original As Point
Private OriginaSize As Size

This are the original position and size of out Car Smile
now add the arguments that the Builder(the new sub) will Get!

This is our New Sub :

Sub New(ByVal OriginalPosition As Point, ByVal CarSize As Size)

Original = New Point(OriginalPosition)
OriginaSize = New Size(CarSize)
End Sub

Now , let's code the functions that will make the car :
we will play with ( X & Y ) , X = horizontal, Y = Vertical

Add 2 Private Functions :

Code:
Private Function _X()
Return OriginaSize.Width
End Function
Private Function _Y()
Return OriginaSize.Height
End Function

functions which sends back to us the size in car's width (to know what is the value of the compartment in width).

Now create a new Funtion (Public)
This function will make our care move forward with the same size and :

Code:
Public Sub Right()
Original.X = Original.X + _PasX()
End Sub

Explaining The code : When you click the button it will call the original position and then move the car with the same information from the Builder using the propertie.X

Now we will work on properties :
This is the code of the propertie :

Code:
Public Property Position()
Get

End Get
Set(ByVal value)

End Set
End Property

What is Get and Set ?
Get : Will be called when we shall ask for a value to this property
Set : Will be called when we shall assign a value to this property

This is our Propertie :
Code:
Public Property Position() As Point
Get
Return Original
End Get
Set(ByVal value As Point)
Original = value

End Set
End Property

Our property is only going to assign the value to the variable Original and to send back(to dismiss) its value.
We assign the value, we return it.

Ok now let's go to out formSmile
Declare a new car :
Dim MyCar As Car

Double click on the form and add :

Code:
MyCar = New Car(Me.PAN.Location, Me.PAN.Size)

Double click on the button that will make the car move forward and add :

Code:
MyCar.Right() ' this will make the car move forward
Me.PAN.Location = MyCar.Position 'this will get back the original position of the car

Now This is the Code of the class :
Code:
Private Original As Point
Private OriginaSize As Size

Public Property Position() As Point
Get
Return Original
End Get
Set(ByVal value As Point)
Original = value

End Set
End Property

Private Function _X()
Return OriginaSize.Width
End Function
Private Function _Y()
Return OriginaSize.Height
End Function
Sub New(ByVal OriginalPosition As Point, ByVal CarSize As Size)

Original = New Point(OriginalPosition)
OriginaSize = New Size(CarSize)
End Sub

Public Sub Right()
Original.X = Original.X + _X()
End Sub

Public Sub Left()
Original.X = Original.X - _X()
End Sub

Public Sub UP()
Original.Y = Original.Y - _Y()
End Sub

Public Sub Under()
Original.Y = Original.Y + _Y()
End Sub

End Class

End Class

The Code Of The Form :
[code]Public Class Form1
Dim MyCar As Car

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyCar = New Car(Me.PAN.Location, Me.PAN.Size)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyCar.Right()
Me.PAN.Location = MyCar.Position
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MyCar.Left()

Me.PAN.Location = MyCar.Position
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
MyCar.UP()
Me.PAN.Location = MyCar.Position
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MyCar.Under()
Me.PAN.Location = MyCar.Position
End Sub
Looks cool, i am going to learn about it in a few weeks, then vacation will be over and they are going to teach this in school.