Re: Newbie question: Writing your own class



I'm not sure where you're coming from (what languages) but some basic
concepts of Delphi OO construction and use are ...

A class is a template for a actual instance of an object. The instance
is made by a class's Create constructor method which allocates memory
for that object. The instance's memory is freed by a Destroy destructor
method, which is not called externally. That is done by a Free method
(called externally) which checks for non-nullity of the instance before
calling Destroy.

Classes "descend" from other classes, and the single original
pre-historic ancestor of all classes is TObject (note that " = class;"
is the same as " = class(TObject)").

Classes have methods (procs & functions), properties and fields.

A single chunk of executable code is provided for all instances of an
object (a reference to the instance is provided by a hidden parameter
named "Self"). Properties have separate storage for every instance.
Properties are accessed either directly of via an "accessor method".
Field are variable internal to the object.

So your class should/could be ...

TTest = class(TObject)
private
FSomeDef : string;
FDefinition : string;
function GetDefinition : string; // accessor get method
procedure SetDefinition(AValue : string); // accessor set method
public
property Definition : string read GetDefinition write
SetDefinition; // access by accessor method
property SomeDef : string read FSomeDef write FSomeDef; // direct
access
end;

constructor TTest.Create;
begin
inherited Create; // not necessary for Tobjec descendants but for
every other descendant of TObject
end;

function TTest.GetDefinition : string;
begin
Result := FDefinition;
end;

procedure TTest.SetDefinition(AValue : string);
begin
if AValue <> FDefinition then
FDefinition := AValue;
end;

var
MyTest : TTest; // variable to hold an instance reference

procedure TForm1.Button1Click(Sender : TObject); // some initiating
event handler
begin
MyTest := TTest.Create;
MyTest.Definition := 'MyDefinition';
MyTest.SomeDef := 'Some Def';
end;

procedure TForm1.Button2Click(Sender : TObject); // some other
displaying event handler
begin
ShowMessage('Definition : ' + MyTest.Definition + #13 +
'SomeDef : ' + MyTest.SomeDef);
end;

procedure TForm1.FormClose(Sender : TObject);
begin
MyTest.Free; // free the instance you have created - a TObject (your
class' parent) has a Free method
end;

The form you are doing this test on is assumed to be Form1. "Get",
"Set", "F", and "T" are prefixes used by convention in Delphi.

You will see that the set & get methods or the "F"-prefixed field
variables are used to hide direct access to the property. The "private"
and "public" keywords are used by the compiler to limit access to the
fields and methods (hiding access to elements of the object are
paradigms of OO).

Enjoy <g>.

Alan Lloyd

.



Relevant Pages

  • Re: dynamic object creation
    ... > var classname: string; ... which constructor will be called. ... ClassRef: TClass; {class of TObject} ... use a string for the "class reference", ...
    (comp.lang.pascal.delphi.misc)
  • =?iso-8859-1?Q?Re:_Memo_wird_nicht_gel=F6scht?=
    ... constructor Create; ... procedure Button1Click(Sender: TObject); ... procedure TWorkerThread.Display(const Msg: string); ...
    (de.comp.lang.delphi.misc)
  • Re: a method to make js have the ability to inherit
    ... but without the implied type-conversion of the string ... that uses the name of a specific constructor. ... programmer has no idea at all what types of object they are ... no reason for ever doing so. ...
    (comp.lang.javascript)
  • Re: About speed
    ... property Name: String read fName; ... constructor TProperty.Create; ... fValue: PropertyOfT_DataType; ... constructor Create(other: TPropertyOfT); overload; ...
    (borland.public.delphi.non-technical)
  • Re: a method to make js have the ability to inherit
    ... discriminating but without the implied type-conversion of the string ... makes no use of an object's - constructor - property anyway.) ... programs should serve some known purpose known to the programmer. ... var o = Father.prototype; ...
    (comp.lang.javascript)