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.