Board index » cppbuilder » Empty Event Handlers

Empty Event Handlers


2004-07-01 02:48:16 AM
cppbuilder25
Why doesn't this work?
...
if ( this->FOnHeaderClick)
this->FonHeader(Sender)
...
Here is the help example:
DevGuide: Component writer's guide
Example: Calling an event handler
An empty handler should produce the same result as no handler at all. So the
code for calling an application's event handler should look like this:
if (OnClick)
OnClick(this);
// perform default handling }
You should never have something like this:
if (OnClick)
OnClick(this);
else
// perform default handling
I had to do this instead:
...
try
{
this->FOnHeaderClick((TObject*) Sender);
}
catch (EAccessViolation& e)
{
}
catch ( ... )
{
throw;
}
...
--
John Runyan
 
 

Re:Empty Event Handlers

"John Runyan" < XXXX@XXXXX.COM >wrote in message
Quote
Why doesn't this work?
Why do you think it doesn't work? Please clearify.
Gambit
 

Re:Empty Event Handlers

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

Why do you think it doesn't work? Please clearify.

I received an EAccessViolation Exception.
--
John Runyan
 

{smallsort}

Re:Empty Event Handlers

"John Runyan" < XXXX@XXXXX.COM >wrote in message
Quote
I received an EAccessViolation Exception.
Where is FOnHeaderClick declared? Where is it actually assigned? Please
show a more complete code snippet.
Gambit
 

Re:Empty Event Handlers

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

Where is FOnHeaderClick declared? Where is it actually assigned? Please
show a more complete code snippet.

Package Code:
class PACKAGE TNNavigationPanel : public TCustomPanel
{
private:
...
TNotifyEvent FOnHeaderClick;
...
__property TNotifyEvent OnHeaderClick = { read=FOnHeaderClick,
write=FOnHeaderClick };
};
//--------------------------------------------------------------------------
-
__fastcall TNNavigationPanel::TNNavigationPanel(TComponent* Owner)
: TCustomPanel(Owner)
{
...
this->FHeader->OnClick = this->NNavigationHeaderPanelClick;
}
//--------------------------------------------------------------------------
-
...
void __fastcall TNNavigationPanel::NNavigationHeaderPanelClick(TObject*
Sender) const
{
if ( this->FOnHeaderClick )
this->FOnHeaderClick(Sender);
}
Design time:
Add TNNavigationPanel
Assign OnHeaderClick to NNavigationPanel1HeaderClick
void __fastcall TForm1::NNavigationPanel1HeaderClick(TObject *Sender)
{
ShowMessage("The Header was clicked!");
}
This works. But if I don't assign anything to OnHeaderClick the
EAccessViolation is thrown.
--
John Runyan
 

Re:Empty Event Handlers

"John Runyan" < XXXX@XXXXX.COM >wrote in message
Quote
void __fastcall TNNavigationPanel::NNavigationHeaderPanelClick(TObject*
Sender) const
{
if ( this->FOnHeaderClick )
this->FOnHeaderClick(Sender);
}
Get rid of the 'const' altogether. Also, you should be using 'this' instead
of 'Sender' when calling FOnHeaderClick:
this->FOnHeaderClick(this);
Asside from that, there is nothing in the code you have shown that would be
causing an access violation.
Quote
This works. But if I don't assign anything to
OnHeaderClick the EAccessViolation is thrown.
That is not possible given the code you have shown. Something else has to
be happening to corrupt your FOnHeaderClick function pointer.
Gambit
 

Re:Empty Event Handlers

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
Get rid of the 'const' altogether. Also, you should be using 'this'
instead
of 'Sender' when calling FOnHeaderClick:
>This works. But if I don't assign anything to
>OnHeaderClick the EAccessViolation is thrown.

That is not possible given the code you have shown. Something else has to
be happening to corrupt your FOnHeaderClick function pointer.
The 'const' was the problem. Without it all works well.
Thanks!
--
John Runyan