PDA

View Full Version : 5 Minute Crash Course On OOP



T.Jackson
- 26th March 2008, 05:00
I realize that there's literally dozens of people on here using VB6, and I also know that most VB6 programmers seldom take advantage of VB's object-based traits. So here's a quick crash course for all those in concern that hopefully may help to break the cycle and aid in a less painful transition from .com to .net What I have listed below is only half of it, perhaps mister_e might like to add further to it, since he is actually now working with .net

So what's an object?

An object is anything and everything. Take the desk you're sitting at for example; you're sitting on a chair, you're looking at a monitor, you're typing on a keyboard, that's 3 out of probably dozens of objects that you have surrounding you.

Ok that was easy, but what's a property?

Loosely speaking properties describe an object. Take your chair for instance, it has legs, it has a seat and it might also have side rails. These are all primary properties of the the chair object, but there's actually a lot more. The legs for instance might have wheels, this being a property of the legs. The idea here really is to break each property down into as many single pieces as possible, you'll see why later ...

What's a method?

Methods are used to manipulate the behavior of an object. Often methods rely on altering the object's properties to do this. For example: a text field has properties like; font, foreground and background colour, width, height -- just to name a few. A method might be used to change say the foreground colour. Recalling our chair for a minute, a method might be created to do something really cool like make it move or spin around.

What's a class?

All objects are derived instances from a class. A class contains all the information about an object. Lets have a look at the chair class below for Java, then we'll see how something similar can be done in VB6, yes that's right VB6!



public abstract class Chair
{
private int legs;
private int seat;
private int rails;

// Constructor
public Chair (int legs, int seat, int rails)
{
this.legs = legs;
this.seat = seat;
this.rails = rails;
}

// Accessor (accessors fetch / return the value)
public int getNumOfLegs()
{
return legs;
}

// Mutator (mutators modify / set the value)
public void setNumOfLegs(int value)
{
legs = value;
}
}


While it is true that VB6 isn't an object orientated language, because it doesn't allow for polymorphism or inheritance of classes, it can still be used to create object-based programs that turn out to be far more tidy and reusable than resorting to procedural methods.




'- VB6 Chair class -'

' Instance variables ...

Private mLegs as Integer
Private mSeat as Integer
Private mRails as Integer

' Accessor in VB is 'Get' (accessors fetch / return the value)
Public Property Get legs() As Integer
legs = mLegs
End Property

' Mutator in VB is 'Let' (mutators modify / set the value)
Public Property Let name(value As String)
mLegs = value
End Property



Ok what about a constructor then?



Private Sub constructor(id as integer, legs as integer, seat as integer, rails as integer)

Chair(id).legs = legs
Chair(id).seat = seat
Chair(id).rails = rails

End Sub


*Place anywhere in your program that you like.

What's polymorpishm and inheritance?

Polymorpishm is a powerful mechanism that allows objects to be processed differently according to their type. Unlike VB6, VB.net allows classes to built using other classes, this is called inheritance. Essentially you inherit all of the methods in your new class from the class you just built on. Take the chair class for example, another class called say the "executive chair" could be build using the chair class as a "baseline". This "baseline" is referred to as the "super class" while the executive chair is a "subclass" Another class could be derived from the chair class called say the "leather chair" If we were to create an instance of both the executive and leather chair classes then we would need to rely on polymorphism in order to know which methods to invoke for each instance of the class.

*Find attached a VB6 project which gives a good example on object-based programming.

mister_e
- 27th March 2008, 23:15
Euh yeah i use .NET and few others... but i'm a pretty poor teacher... and learned my own way... I would prefer answer question instead and bring some personal solutions.

Nice crash course anyways!

T.Jackson
- 28th March 2008, 07:13
but i'm a pretty poor teacher... and learned my own way...


Oh I think you are, an excellent teacher from what I've seen.