Solved

Classes (member functions returning obj of same class?)

Posted on 2002-06-20
5
228 Views
Last Modified: 2010-04-04
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):TString;
    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
0
Comment
Question by:barbourwill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7097265
Short answer yes... long answer: yes, but it can get real dicey. Take a look:

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: TObject);
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;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7097303
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):TString;
   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,Amount);
end;

Procedure TString.Append(sStr:TString);
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.
0
 
LVL 1

Author Comment

by:barbourwill
ID: 7097395
ok that sounds good, i thought it may be *bad* programming practice or something!


will
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
ID: 7097422
>it may be *bad* programming practice
never heard this in this context

also the vcl do such things

meikl ;-)
0
 
LVL 1

Author Comment

by:barbourwill
ID: 7097435
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 155
Delphi and Access based Enumeration 9 87
Tvertscrollbox like a whatsapp layout 5 68
Problem working with dynamic array - help 2 48
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question