Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to pass local variables to an EventHandler


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Nov 10, 2015 2:31 AM Last Post By: Patricio Cerda
Patricio Cerda

Posts: 122
Registered: 3/13/01
How to pass local variables to an EventHandler  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2015 5:23 PM
Hi,

Is it possible to pass parameters to an event method? Which is the correct way to do that in C++?
I want to pass some parameters to the event method that came with a third party component, but avoiding global variables, using instead local variables in the context of code where the component is used.

This is a specific example related with TfrxReport that came with an OnGetValue event handler as follows:

void __fastcall TFormDocs::BitBtnProdsClick(Object *Sender)
{
	String ValorA = "Wanted kind of variable"; // Locally defined variables I would like to use.
	String ValorB = "Local variable"; // These are not known later in the OnGetValue event handler
	...
	frxRep_TicketProd->ShowReport(true);
	...
}
 
void __fastcall TFormDocumentos::frxRep_TicketProdGetValue(const UnicodeString VarName,
		  Variant &Value)
{
	if(CompareText("ParamA", VarName) == 0)
		Value = ValorA; // Here, the ValorA value is undefined, unless I declare it as a global variable.
	else if(CompareText("ParamB", VarName) == 0)
		Value = ValorB;  // Same as before.
}


I would appreciate it if someone could teach me on this matter.

Best regards,
Patricio Cerda
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to pass local variables to an EventHandler  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2015 5:40 PM   in response to: Patricio Cerda in response to: Patricio Cerda
Patricio wrote:

Is it possible to pass parameters to an event method?

No. Existing events have defined signatures that cannot be changed.

I want to pass some parameters to the event method that came with
a third party component, but avoiding global variables, using instead
local variables in the context of code where the component is used.

You can store custom values in the component's Tag property, eg:

struct sValor
{
    String ValorA;
    String ValorB;
};
 
void __fastcall TFormDocs::BitBtnProdsClick(Object *Sender)
{
    sValor Valor;
    Valor.ValorA = "Wanted kind of variable"; // Locally defined variables 
I would like to use.
    Valor.ValorB = "Local variable"; // These are not known later in the 
OnGetValue event handler
    ...
    frxRep_TicketProd->Tag = reinterpret_cast<NativeInt>(&Valor);
    frxRep_TicketProd->ShowReport(true);
    ...
}
 
void __fastcall TFormDocumentos::frxRep_TicketProdGetValue(const UnicodeString 
VarName, Variant &Value)
{
    sValor *Valor = reinterpret_cast<sValor*>(frxRep_TicketProd->Tag);
 
    if (CompareText("ParamA", VarName) == 0)
        Value = Valor->ValorA;
    else if (CompareText("ParamB", VarName) == 0)
        Value = Valor->ValorB;
}


--
Remy Lebeau (TeamB)
Patricio Cerda

Posts: 122
Registered: 3/13/01
Re: How to pass local variables to an EventHandler  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 2:31 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
No. Existing events have defined signatures that cannot be changed.
Understood. I supposed that, but not sure.

You can store custom values in the component's Tag property, eg:

struct sValor
{
    String ValorA;
    String ValorB;
};
 
void __fastcall TFormDocs::BitBtnProdsClick(Object *Sender)
{
    sValor Valor;
    Valor.ValorA = "Wanted kind of variable"; // Locally defined variables 
I would like to use.
    Valor.ValorB = "Local variable"; // These are not known later in the 
OnGetValue event handler
    ...
    frxRep_TicketProd->Tag = reinterpret_cast<NativeInt>(&Valor);
    frxRep_TicketProd->ShowReport(true);
    ...
}
 
void __fastcall TFormDocumentos::frxRep_TicketProdGetValue(const UnicodeString 
VarName, Variant &Value)
{
    sValor *Valor = reinterpret_cast<sValor*>(frxRep_TicketProd->Tag);
 
    if (CompareText("ParamA", VarName) == 0)
        Value = Valor->ValorA;
    else if (CompareText("ParamB", VarName) == 0)
        Value = Valor->ValorB;
}
Excellent! It works perfect and gave my a way to do what I want.

Thank you very much Remy.

Best regards,
Patricio Cerda
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02