Board index » delphi » Calling Event Handler w/More than one Variable?

Calling Event Handler w/More than one Variable?

How do I call Event Handler that has more than one parameter, from within
another Event Handler?

Example:

procedure TfrmSong.cmbFilter(Sender: TObject; var Selection: String);
begin
    code;
end;

Now, how do I call the above handler from another?  I am trying the
following but does not work:

begin
    cmbFilter(Sender, 'None');
end;

Thanks in advance!

Roger

 

Re:Calling Event Handler w/More than one Variable?


Roger,
  what exactly does not work?  Your event code is obviously stripped out,
and you don't say what happens when you do this.  Are you getting a compile
error?  An access violation?  Post more info and we'll try to help...
-Howard

Re:Calling Event Handler w/More than one Variable?


If I place a button on form and in the OnClick event place the code:
begin
     cmbFilterSelection(Sender, 'None');
end;

Trying to call a previously used Handler in same .pas:

procedure TfrmSong.cmbFilterSelection(Sender: TObject; var Selection:
String);
begin
    whatever code;
end;

I get a compile error that says:
    "Constant object cannot be passed as var parameter"

Thanks for the reply!

Quote
Howard Moon <hm...@landstar.com> wrote in message

news:8m72ei$i945@bornews.borland.com...
Quote
> Roger,
>   what exactly does not work?  Your event code is obviously stripped out,
> and you don't say what happens when you do this.  Are you getting a
compile
> error?  An access violation?  Post more info and we'll try to help...
> -Howard

Re:Calling Event Handler w/More than one Variable?


I think I figured this out.  You must declare a variable inside the event
from which you call the other event and assign it the value you want to use
when calling the event that you are accessing.
Example:
procedure whatever;
var what: string;
begin
    what := none;
     cmbFilterSelection(Sender, what);  {I was trying to put a string here.
Can't do.}
end;
Trying to call a previously used Handler in same .pas:

procedure TfrmSong.cmbFilterSelection(Sender: TObject; var Selection:
String);
begin
    whatever code;
end;

Quote
Roger Mudgett <sha...@mail.airmail.net> wrote in message

news:398709ae_2@dnews...
Quote
> How do I call Event Handler that has more than one parameter, from within
> another Event Handler?

> Example:

> procedure TfrmSong.cmbFilter(Sender: TObject; var Selection: String);
> begin
>     code;
> end;

> Now, how do I call the above handler from another?  I am trying the
> following but does not work:

> begin
>     cmbFilter(Sender, 'None');
> end;

> Thanks in advance!

> Roger

Re:Calling Event Handler w/More than one Variable?


Hi Roger,

Quote
> I get a compile error that says:
>     "Constant object cannot be passed as var parameter"

Your error message is very explicit. You try to pass a constant string
('None') to a var parameter (Selection). This cannot work, because a var
parameter must be able to change in the course of the  procedure. If you
really want to keep Selection as a var parameter, you should have something
like this for your OnClick event:

var MyString : String;
begin
    MyString := 'None';
    cmbFilterSelection(Sender, MyString);
end;

Thrse

Re:Calling Event Handler w/More than one Variable?


Quote
> I think I figured this out.  You must declare a variable inside the event
> from which you call the other event and assign it the value you want to
use
> when calling the event that you are accessing.

Sort of, Roger..  but don't misinterpret that - you'd have to declare the
variable regardless of where the method existed, or even whether it was a
standalone function or procedure, because the parameter used the "var"
(variable) keyword.  You tried to pass it a constant, which caused the
error.  There are four ways to classify a parameter (five, if you count
untyped) : var, const, out, and value (default).

Remove the "var" from the procedure declaration like this:
  procedure TfrmSong.cmbFilterSelectionSender: TObject; Selection: String);
and you can call it with either the constant or the variable.
Change it to
  procedure TfrmSong.cmbFilterSelectionSender: TObject; const Selection:
String);
and you can use either one, but the procedure cannot modify the "Selection"
parameter.
Change it to
  procedure TfrmSong.cmbFilterSelectionSender: TObject; out Selection:
String);
and you cannot use the constant (you must declare and use a variable), but
the procedure never gets to see the value of the parameter - it can modify
it, though.

--
David Reed
Phoenix, AZ (temporarily)

Re:Calling Event Handler w/More than one Variable?


Quote
> If I place a button on form and in the OnClick event place the code:
> begin
>      cmbFilterSelection(Sender, 'None');
> end;

  Var S: String;
begin
  cmbFilterSelection(Sender, S);
end;

HTH
--
V. Nazarov
IT Manager, Sofia Cable

Other Threads