IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesDelphiOOP Basics  Print This     

Delphi OOP Basics

Class..Object

Languages Focus

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

Delphi:   class..end..Create

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, call the class constructor (usually named Create). Since Delphi does not have a garbage collector, you have to also free the object usually with either Free or FreeAndNil.

Syntax Example:
//Interface section:
TCyborg = class(TObject)
public
procedure IntroduceYourself;
end;
 
//Implementation section;
procedure TCyborg.IntroduceYourself;
begin
ShowMessage('Hi, I do not have a name yet.');
end;
 
//Some event like a button click:
var
T1: TCyborg;
begin
T1 := T1.Create;
T1.IntroduceYourself;
  FreeAndNil(T1);      //Be sure to clean up!
end;




Cross Reference Examples:

ASP Classic:   Class..Set..New

Ultra-primitive (no inheritance) but useful and encourages you to think and design using objects. Unlike VB, you can have more than one class per file.

Classes in ASP do support member fields, properties, and methods.

Syntax Example:
'Declare class.
Class Cyborg
  Public Function IntroduceYourself() 
    Response.Write("Hi, I do not have a name yet.") 
  End Function 
End Class
 
'Create object from class.
Set T1 = new Cyborg
T1.IntroduceYourself() 
Set T1 = Nothing      'Be sure to clean up!
C#:   class...new

In C#, you use the class keyword to specify a class and you signify its parent with a colon and the name of the parent class. When you instantiate an object from a class, you use the new keyword.

Syntax Example:

Define class:

public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, I do not have a name yet.");
}
}

Create object from class:

Cyborg T1 = new Cyborg();
T1.IntroduceYourself();
//No need to clean up with managed classes.
//The garbage collector will take care of it.
C++:   Yes More Info / Comment
Corel Paradox:   Not Supported
Delphi Prism:   class..end..new

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, use the New keyword. Optionally, you can use Create for backword compatibility with Delphi if you turn it on in the compatibility options. Since Prism does have a garbage collector, you do not have to free the object. If you need to free either unmanaged resources or resources where "timing" is important, implement IDisposable and take control of freeing the object yourself using Dispose.

Syntax Example:

In the interface section:

Cyborg = class(System.Object)
public method IntroduceYourself();
end;

In the Implementation section:

method Cyborg.IntroduceYourself();
begin
MessageBox.Show("Hi, I do not have a name yet.");
end;

On some event like a button click:

var T1: Cyborg;
begin
T1 := New Cyborg;
T1.IntroduceYourself;
  //No need to clean up with managed classes.
  //The garbage collector will take care of it.
end;
Java:   class..new

Unlike languages such as C++ and Object Pascal, every line of code written in Java must occur within a class.

Syntax Example:
//Declare class.
public class Cyborg {
  //Fields.
  private String cyborgName;
  private int age;
 
  //Constructor.
  public Person() {
  cyborgName = "unknown";
  age = 0;
  }
}
//Create object from class.
Cyborg p = new Cyborg();
p.getClass(); //From the Object base class.
JavaScript:   Limited, class..new

Creating classes in JavaScript is not really OOP, but rather a super type. That is, a type that has some class-like features but is missing the necessary OOP requirements.

There is nothing in Javascript to stop you from accessing the functions within your class outside of the class so this is not fully OOP but is usable.

Syntax Example:
//Class definition.
function Person() {
this.name = 'unknown';
this.age = 0;
}
 
//Use object created from class.
var Lisa = new Person();
Lisa.name='Lisa';
Lisa.age=28;
VB Classic:   Yes

One class per file. File must use a .cls extension (instead of .bas). You can include your classes in your project (a traditional OOP approach) or compile them to a DLL or ActiveX EXE, register them, and use them that way (a uniquely VB approach).

More Info / Comment
VB.Net:   Class..End Class..New

Declare and implement VB.Net classes after the form class or in their own .vb files. Unlike VB Classic, you can have more than one class in a .vb class file (VB classic uses .cls files for each class).

Syntax Example:

Class definition:

Public Class Cyborg
  Inherits Object
 
  Public Sub IntroduceYourself()
  MessageBox.Show("Hi, I do not have a name yet.")
 End Sub
End Class

Some event like a button click:

Dim T1 As New Cyborg
T1.IntroduceYourself()
//No need to clean up with managed classes.
//The garbage collector will take care of it.




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]