5 Minute Crash Course On OOP


Closed Thread
Results 1 to 3 of 3
  1. #1
    T.Jackson's Avatar
    T.Jackson Guest

    Post 5 Minute Crash Course On OOP

    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!

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

    Code:
    '-  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?

    Code:
    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.
    Attached Files Attached Files
    Last edited by T.Jackson; - 26th March 2008 at 06:21.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    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.

Similar Threads

  1. PICs can do more if use others than delays instructions
    By hardcore in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th February 2010, 20:52
  2. RFID medicine teller!! problem with programming
    By sknee8787 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 18th November 2008, 18:31
  3. LCD will not start
    By btaylor in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 24th May 2007, 03:30
  4. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 20:03
  5. Replies: 11
    Last Post: - 13th July 2005, 20:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts