I.T. Discussion Community!
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

February Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Class..Object (class..end..new)
 
Posted 11 years ago on 12/19/2008 and updated 2/22/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101731



Languages Focus: Class..Object

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 Prism Class..Object

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;

Add a Property

Let's give our cyborg a name:

//Interface section:
Cyborg = class(System.Object)
public
property Name: String;
method IntroduceYourself();
end;
 
//Implementation section:
method Cyborg.IntroduceYourself();
begin
MessageBox.Show("Hi, my name is " + Name + ".");
end;

Now let's use our new class in some event like a button click:

var 
  T1: Cyborg;
begin
  T1 := New Cyborg;
  T1.Name := "Number 1";
  T1.IntroduceYourself;
end;

Complete Example

Assuming you have a form with a button on it, here's the complete listing of the form code:

namespace Class_Object;
 
interface
 
uses
  System.Drawing,
  System.Collections,
  System.Collections.Generic,
  System.Linq,
  System.Windows.Forms,
  System.ComponentModel;
 
type
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  MainForm = partial class(System.Windows.Forms.Form)
  private
    method button1_Click(sender: System.Object; e: System.EventArgs);
  protected
    method Dispose(disposing: Boolean); override;
  public
    constructor;
  end;
   
  Cyborg = class(System.Object)
  public 
    property Name: String;      //Using shortcut syntax.
    method IntroduceYourself();
  end;
 
implementation
 
{$REGION Construction and Disposition} 
constructor MainForm;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
method MainForm.Dispose(disposing: Boolean);
begin
  if disposing then begin
    if assigned(components) then
      components.Dispose();
    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(disposing);
end; 
{$ENDREGION}
 
method Cyborg.IntroduceYourself();
begin
  MessageBox.Show("Hi, my name is " + Name + ".");
end;
 
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
var
  T1: Cyborg;
begin
  T1 := New Cyborg;
  T1.Name := "Number 1";
  T1.IntroduceYourself;
end;
 
end.

Use the Legacy Create Constructor

If you turn on legacy support for Create, you can use the Delphi-style Create to instantiate objects:

var
T1: Cyborg;
begin
T1 := Cyborg.Create;
T1.CyborgName := "Number 1";
T1.IntroduceYourself;
end;

To turn on legacy support for Create, in the solution explorer right click on your solution or project and select Properties. On the Compatibility tab, check Allow 'Create' Constructor calls.

Allow Create constructor calls.

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

Two criticisms. 

1. At the very beginning, I would SHOW the Type keyword before the type definition.  Otherwise it looks like it isn't required.  But it IS.  It is just part of the Type statement in later sample code.  And I would even put the Type in front of the class definition shown later.  It isn't required, but it helps readability.  Particularly if you indent the Type statements at the same level.  I would also use vertical white space to separate the types.  Again for readability.

2. Type definitions don't HAVE to go in the Interface section.  They can also go in the Implementation section.  Where it goes depends on scope.  If you want to expose the type, then put it in the Interface.  But, if it is just a type that is used within the scope of Implementation (like some sort of helper class, for instance), then put it in the Implementation.

The description makes it sound too much like the Type declaration MUST go in the interface.

I understand this is just a basic introduction, but I would be careful not to imply incorrect information.

Otherwise, good job.

Posted 7 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1182A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101731 Counter
13365
Since 12/19/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,083 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech