Objects

Instead of variables, subroutines and functions, objects have parameters, methods, and events.

Visual Basic | Delphi | C++ Builder | Java

Visual Basic 6.0

Basically, a class definition is just like a .bas module except that the file extension is .cls and there are a few extra hidden headers in the file. As a result, there can be only one class defined per file.

To create a new Class Definition file, from the VB menu, select Project / Add Class Module.


Help Topics

Pretty good help is available, but good luck finding it. The following are some help screens which I found useful. Use the up and down arrows on the toolbar to find additional info.
objects  properties vs. methods
This is an index to several interesting pages.
objects  standard module life cycle
This contains a good fully explained example which demonstrates how 2 instances of the same class contain different data. There is also a section on Static Class Data and how to fake it.
programming  object basics / Creating Your Own Classes / Class Module Step by Step
This is a very good step-by-step example where you copy and paste code to a form. The explanation was great.
polymorphism  defined
Unlike object-oriented languages, there is no such thing as an abstract class and, therefore, VB doesn't use inheritance to provide polymorphism. However, there is an Implements BaseClass command, and a kludge to allow calls to the baseclass to be executed by the derived class.
(Uh, the bold headings above have been fixed so that you can copy them to the clipboard and paste them to the help system. Basically, for this to work, there must be 2 spaces between the main topic and the sub topic. In the VB help application, select Index, then copy and paste the bold identifiers. Then either press enter or double click the highlighted selection.)



Defining a Property

To set a default property, use Tools / Procedure Attributes... / Advanced>> and set Procedure ID to (Default). Be very sure that only one method or property is defined as the default since the compiler does not check for multiples.

You cannot implement true static class data which is available to all objects of the same class. Instead, you have to fake it by defining a public variable in another module (.bas file) and use class methods to access it.


Instantiating an Object

There are 3 ways to instantiate an object An object's memory is automatically released when there are no more references to it.
    Set x = Nothing


Delphi

From the Delphi Class type example.
TField = class
private
  X, Y, Len: Integer;
  FName: string;
  FStr:  string;
public
  constructor Copy(F: TField);
  constructor Create(FX, FY, FLen: Integer; FName: string);
  destructor  destroy; override;
  procedure   Display; virtual;
  procedure   Edit   ; dynamic;
protected
  function  GetStr: string; virtual;
  function  SetStr(S: string): Boolean; virtual;
  procedure Something; virtual; abstract;

private
  procedure DisplayStr(X, Y: Integer; S: string);
public
  property Str: String read GetStr write SetStr; // Methods must be defined 
                                                 //   before they are used
                                                 //   in a property 
published
  property Cnt: Integer read GetCnt write SetCnt default 30;  
end;
Properties that are defined as published are displayed in the Object Inspector at design time and can be modified by the developer.

In a property, the read directive must come before the write directive.

The default directive is used with numbers, if the value in the object inspector is different from the default value, then the value is saved in the *.dfm file. It does not actually set the value of the property. You must assign all data fields to their default values in the component's constructor(s). String properties can not have a default directive, and all strings <>'' are stored in the *.dfm file. ref

type
  PTRectangle = ^TRectangle;   // Pointers to a type can be defined before the type
  TRectangle = class(TFigure)
    procedure Draw; override;
    :
  end;
  TEllipse = class(TFigure)
    procedure Draw; override;
    :
  end;
The following section of code illustrates the effect of calling a virtual method through a class type variable whose actual type varies at run-time.
var
  Figure: TFigure;                      // This does not allocate any memory
begin
  Figure := TRectangle.Create;          // This allocates memory
  Figure.Draw;				{ Invokes TRectangle.Draw }
  Figure.Destroy;
  Figure := TEllipse.Create;
  Figure.Draw;				{ Invokes TEllipse.Draw }
  Figure.Destroy;
end;
The allowed subroutine (method) types are Functions and procedures can be defined as virtual and override. Derived classes can override virtual funtions declared in a base class. When a variable is declared (or cast) as a base class type, then references to a virtual function produce an indirect branch through a table to the appropriate function for the derived type. (How's that for confusing double talk.)

Abstract virtual methods do not have any code defined. These must be overridden before they are called.

inherited - Calling inherited with an abstract virtual method will generate a run time exception.

type
  TMyType = record
    a: string;
    b: string;
  end;
var
  MyArray :  Tlist;
  MyType  :  Tmytype;
  PMyType : ^Tmytype;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyArray:= tlist.create;
  new (Pmytype);          // This allocates memory and initializes the pointer
  MyArray.add(PMyType);
end;

When designing components, use this to keep methods from running at design time.

Multiple inheritance is partially supported using interfaces - special classes which contain only abstract methods.

Methods (function and procedures) can be defined as class methods which can be accessed without specifying an instance (object).

  class function  SetStr(S: string): Boolean; virtual;
  class procedure Something; virtual; 

  tMyType.something; // This will call the procedure


C++ Builder

If you only want to store data, you can use a structure. The default access is public.
struct SomeName{
  char  str1[20];
  char  str2[4];
  short x;
  short y;
};                     // The semicolon is required
                       // Instances can be created in the definition by placing
                       //   variable names before the final semicolon

SomeName var1;         // Declare a variable of that type
SomeName varArray[4];  // An array of structures

strcpy(var1.str1, "A string");
var1.x = 35;
varArray[2].y = 66 ;
A class is just like a structure except that the default access is private.

Allows multiple inheritence.


Java

public class NewName extends ExistingClass{
  public integer someValue   = 5;
  static int     someCounter = 0; // defines a "Class Variable" who's value
                                  // is available in all instances of this class
  public void method1() {
      this.someValue    = 8 ; // Variable in this class
     super.anotherValue = 3 ; // Variable in parent class
  }
  public NewName (int a) {    // Constructors have the same name as the class
      this(null, a) ;         // "this" calls another constructor in this class
                              //   (see JTextField.java for examples)
  }
  public NewName (int a, int b) {    // There can be many constructors
                                     // as long as each one has different parameters
  }

}

NewName ClassInstance = new NewName() ; // allocate some memory
NewName.someCounter++ ; // "Class Variables" are referenced using the class name

ClassInstance.someValue = 4 ;
Java 1.1 lacks property definitions. Instead, you simply make the attribute private and define 2 public methods - one to read and another to write. By convention, most development systems treat getName and setName are interpreted as the parameter name.

static is used to define Class Variables and Methods. These allocate space once regardless of how many instances are allocated. These can be referenced using either the class name or the name of a variable that references a specific instance. You can use Class Methods to read and write Class Variables.

Constructors do not return a value and have the same name of the class.
Classes frequently have multiple constructors.

One of the advertised advantages of java is that multiple inheritance is NOT supported. (A dubious advantage at best.) Then, in standard doublespeak, you are told that multiple inheritance IS supported when interface classes are inherited.

BTW, Java does not support events, it uses interfaces instead. (Same idea, different name.)


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Objects.htm