A "normal" Delphi snippet:

Procedure help_me1(Sender: TObject);
  BEGIN
{100 lines of code}
  END;

Procedure help_me2(Sender: TObject);
  BEGIN
{100 lines of copy and paste code}
  END;

How can I make this so that I end up with ONE procedure with 100 lines of code?

Procedure much_reuseable_code(choice:BYTE);
  BEGIN
{100 lines of code}
  END;

Procedure help_me1(Sender: TObject);
  BEGIN
    much_reuseable_code(1);
  END;

Procedure help_me2(Sender: TObject);
  BEGIN
    much_reuseable_code(2);
  END;

--In old days this was the way to do it, but the Sender: TObject makes it much harder.
I would really appreciate any help in regards to this.
Preferably more than one solution if there are several ways of bypassing the problem.

Thanks in advance.
M.

Recommended Answers

All 4 Replies

Passing a variable like that is the right way. Not sure why you have the sender in the parameter list, unless it is a default event. Some more information on the structure of your code may shed some light on the situation.

I have a form with Delphi 5.
To simplify it, It has got two buttons and a list box.
I am accessing a database to list either first names or last names in the list box.
This is what the buttons are for. the choice of first name or last name.

The events created in Delphi is:

procedure Tlistname.Button1Click(Sender: TObject);
and
procedure Tlistname.Button2Click(Sender: TObject);

and it is the code within these two procedures that I try to simplify with the above method.
The problem is that I then do not get access to many of the things on the form like IBQuery and similar.

I Think I need to pass the (Sender: TObject) declaration somehow, but I seem to be unable to do that.


Maybe the question can be simplified into:::
How do I make a Procedure or a Function that is outside a form
but still has access to the form and its values?
and also, how do I make parameter declarations for this?

Tlistname = class(TForm)
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
public
  procedure much_reuseable_code(AChoice: Integer);
end;

procedure Tlistname.Button1Click(Sender: TObject);
begin
  much_reuseable_code(1);
end;

procedure Tlistname.Button2Click(Sender: TObject);
begin
  much_reuseable_code(2);
end;

procedure Tlistname.much_reuseable_code(AChoice: Integer);
begin
end;
Tlistname = class(TForm)
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
public
  procedure much_reuseable_code(AChoice: Integer);
end;

procedure Tlistname.Button1Click(Sender: TObject);
begin
  much_reuseable_code(1);
end;

procedure Tlistname.Button2Click(Sender: TObject);
begin
  much_reuseable_code(2);
end;

procedure Tlistname.much_reuseable_code(AChoice: Integer);
begin
end;

The PUBLIC declaration in beginning did the trick.
It also work with PRIVATE declaration :-)
Thanks a lot. you are a gem :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.