barbourwill
asked on
Classes (member functions returning obj of same class?)
Say you have a class called TString, which has member functions to do useful stuff like copying, concating,appending etc..
TString=class(TObject)
public
function Copy(Amount:integer):TStri ng;
Procedure Append(sStr:TString);
end
Is this acceptable? can classes have member functions that input/output objects of the same class type?
I would appreciate any help.
thanks a lot!
Will
TString=class(TObject)
public
function Copy(Amount:integer):TStri
Procedure Append(sStr:TString);
end
Is this acceptable? can classes have member functions that input/output objects of the same class type?
I would appreciate any help.
thanks a lot!
Will
yes, why not?
a sample
unit tsring_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString=class(TObject)
private
fstring : string; //or whatever you need for store
public
function Copy(Amount:integer):TStri ng;
Procedure Append(sStr:TString);
published
Property AString : String read fString write FString;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//implementation of the TString Object
Function TString.Copy(Amount : integer) : TString;
begin
//whatever you want to do there
result := TString.Create;
result.AString := system.Copy(fString,1,Amou nt);
end;
Procedure TString.Append(sStr:TStrin g);
begin
//whatever you want to do there
If assigned(sStr) then
fString := fString+sStr.AString;
end;
//end implementation of the TString Object
//usage sample
procedure TForm1.Button1Click(Sender : TObject);
var
s1,s2 : Tstring;
begin
s1 := tstring.Create;
s1.AString := Edit1.Text;
s2 := s1.Copy(3);
s2.Append(s1);
Edit2.Text := s2.AString;
s1.free;
s2.free;
end;
end.
a sample
unit tsring_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString=class(TObject)
private
fstring : string; //or whatever you need for store
public
function Copy(Amount:integer):TStri
Procedure Append(sStr:TString);
published
Property AString : String read fString write FString;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//implementation of the TString Object
Function TString.Copy(Amount : integer) : TString;
begin
//whatever you want to do there
result := TString.Create;
result.AString := system.Copy(fString,1,Amou
end;
Procedure TString.Append(sStr:TStrin
begin
//whatever you want to do there
If assigned(sStr) then
fString := fString+sStr.AString;
end;
//end implementation of the TString Object
//usage sample
procedure TForm1.Button1Click(Sender
var
s1,s2 : Tstring;
begin
s1 := tstring.Create;
s1.AString := Edit1.Text;
s2 := s1.Copy(3);
s2.Append(s1);
Edit2.Text := s2.AString;
s1.free;
s2.free;
end;
end.
ASKER
ok that sounds good, i thought it may be *bad* programming practice or something!
will
will
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You seem to know what your talking about, thanks for the help.
PS.Know anything about programming peer to peer networks such as kazaa? check out my latest question.
PS.Know anything about programming peer to peer networks such as kazaa? check out my latest question.
Type
TMyObject=class
Name:string;
Procedure Foo(str1,str2:TMyObject);
Function Bar:TMyObject;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender
var MyObj,Myobj2:TMyObject;
begin
MyObj:=TMyObject.Create;
MyObj.Name:='DrDelphi';
Myobj2:=MyObj.Bar;
MyOBj2.Name:='To the rescue!!';
MyObj.Foo(myobj,myobj2);
end;
{ TMyObject }
function TMyObject.Bar: TMyObject;
begin
Result:=TMyObject.Create;
end;
procedure TMyObject.Foo(str1, str2: TMyObject);
begin
Showmessage(str1.Name);
Showmessage(str2.Name);
end;