Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Jul 13, 2020 3:16 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: How to: Directly call an event handler (Undocumented?)
PostPosted: Wed May 27, 2009 7:39 pm 
Offline

Joined: Wed Dec 26, 2007 4:31 pm
Posts: 15
So, as far as I can tell, this is undocumented. A common request (e.g. here or here) is to call event handlers directly, as though they were regular functions.

For example, you have a button and a menu item. They both do the same thing. Wouldn't it be nice if the button could call the menu item's handler or vice-versa?

The standard advice is that this is not possible -- you must create a separate function that implements the shared functionality and then call that function from both the button's and the menu item's handlers.

But... it is possible, it's just not documented or officially supported as far as I can tell.

Let's say you have a Window and on it a PushButton called PushButton1. You've put some code in the PushButton's Action() handler. To call the action handler from somewhere else, just do:

Sub SomeFunction()
' Uses undocumented functionality!
PushButton1_Action()
End Sub


Let's say you have a menu item "Buy" in the "File Menu". You've named it FileBuy and added a menu handler for it. You can call that menu handler like this:

Sub SomeOtherFunction()
' Uses undocumented functionality!

' Note the leading underscore: this is needed for calling
' menu handlers, but not control handlers.
_FileBuy_Action()
End Sub


I discovered this by accident trying to do things the "official" way. I had a button called Button1 and wanted to invoke its functionality from several places. So I created a function called Button1_Action and then, in Button1's Action() handler I called Button1_Action(). This didn't work ("Duplicate method definition"), which puzzled me. Eventually I figured out what was going on.

Shouldn't this be documented? Or, if we're not supposed to use this functionality, REALbasic shouldn't be using obvious function names like ButtonName_Action() or should be putting those functions somewhere in its own (inaccessible) namespace.

Really -- REALbasic should just make it official and let us call the handlers! :)


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Wed May 27, 2009 8:41 pm 
Offline

Joined: Wed Feb 15, 2006 1:30 pm
Posts: 3108
Location: U.S.A (often), Creswell, often Dallas, Atlanta, Spokane, Pago Pago.
Quote:
But... it is possible, it's just not documented or officially supported as far as I can tell.

I thought it was officially *not* supported and a phenomenally (pun intended) bad idea.
<big shudder>

Michael

_________________
REAL Studio 2010 R2 Mac and Win, OS X (10.6.2), Win XP SP 3 and Win 7


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Wed May 27, 2009 9:13 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
I agree. I would prefer that the fix the compiler to flag that as an error.

Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 12:06 am 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
Nate wrote:
So, as far as I can tell, this is undocumented.


Undocumented
Unsupported
And a really bad idea to do it in either case

Create a method and call the method

If REAL happens to change things and how they work your advice may lead people to code that will no longer work

Don't do it

_________________
Norman Palardy (Real Software)


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 12:49 am 
Offline
User avatar

Joined: Tue Mar 17, 2009 8:53 am
Posts: 751
Just so we're clear, I 100% agree that one should never rely on undocumented behavior. Nate does have a point though, which is that it should not be an error to name a function Pushbutton1_Action. What if future versions of REALbasic outlaw other function names - ones we're currently already using!?

I would prefer that RB explicitly document that certain function names are reserved for use by the event mechanism, and, further, that RB explicitly document whether or not we are allowed to call them. Once it's written down in the docs, either (a) it becomes legal to call the handler, or (b) it becomes illegal to use certain function names.

_________________
No thanks, I don't want to buy any plugins.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 1:08 am 
Offline
User avatar

Joined: Thu Nov 15, 2007 4:07 am
Posts: 313
Location: 't Zand, The Netherlands
Interesting. I have a number of methods I call Draw_XXXX to send graphics to both a canvas and the printer. Who knows the compiler would have recognized - or will recognize in future versions - some of these names. If RB uses the underscore for internal event handlers, it would probably be best not to allow the underscore at all.

Frank

_________________
Debian Sqeeze - Real Studio 2011r4
You have to have a little programming fun every day!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 8:08 am 
Offline

Joined: Wed Dec 26, 2007 4:31 pm
Posts: 15
Just to be clear, yes it's a bad idea to use this functionality, or anything undocumented, in a real program. I assumed that was a given!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 8:48 am 
Offline

Joined: Fri Sep 30, 2005 8:53 am
Posts: 1002
Frank32 wrote:
If RB uses the underscore for internal event handlers, it would probably be best not to allow the underscore at all.


No it would not for a lot of reasons... One is, IIRC, that every language I have ever used in has allowed underscores in identifiers... And many people do use it.

- Karen


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 10:29 am 
Offline
User avatar

Joined: Thu Nov 15, 2007 4:07 am
Posts: 313
Location: 't Zand, The Netherlands
Karen wrote:
One is, IIRC, that every language I have ever used in has allowed underscores in identifiers... And many people do use it.
I know. The problem, however is that when a language uses one notation for the code editor (like Canvas1.Paint) and uses another internally for the same (Canvas1_Paint), naming conflicts may arise.

A possible solution would be to have all internal identifiers start with an underscore while not allowing programmers to do the same, and prevent programmers from calling them, which is what this thread is about.

Frank

_________________
Debian Sqeeze - Real Studio 2011r4
You have to have a little programming fun every day!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 11:00 am 
Offline

Joined: Fri Sep 30, 2005 8:53 am
Posts: 1002
Frank32 wrote:
A possible solution would be to have all internal identifiers start with an underscore while not allowing programmers to do the same, and prevent programmers from calling them, which is what this thread is about.


That is already true for the most part... and has been for a long time... They just did not follow that for events apparently.

- karen


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 1:35 pm 
Offline
User avatar

Joined: Wed Nov 08, 2006 11:04 am
Posts: 135
Of course, it's always a bad idea using undocumented/unsupported functionality because it's behavior may change without warnings in future.
On the other hand the support of calling event handlers directly would be very welcome, I think.
Therefore Real Software could add value to their product without a lot of work because the functionality is already available.

Dana, it would be great if you could make your engineers aware of that idea.
However, I am sure some of them are following our discussions anyway. :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 3:56 pm 
Offline

Joined: Fri Sep 30, 2005 9:16 am
Posts: 761
Quote:
Dana, it would be great if you could make your engineers aware of that idea.
However, I am sure some of them are following our discussions anyway.


Real does not accept feature requests through the forums. You need to use their website form to do submit a feature request for considerations.

It's just as easy, and one could easily argue better practice given the above discussion, to create a method outside of the event and call the method instead.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 4:48 pm 
Offline

Joined: Sat Apr 25, 2009 4:08 am
Posts: 237
npalardy wrote:

If REAL happens to change things and how they work your advice may lead people to code that will no longer work

Don't do it


VB6 allows it
Delphi allows it

Other than because of the fact that changes to RB may break your code why is this a bad idea from a design standpoint?

_________________
RB 2012 Release 2.1 Personal edition (Windows)


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Thu May 28, 2009 11:52 pm 
Offline
User avatar

Joined: Wed Nov 08, 2006 11:04 am
Posts: 135
pregan wrote:
Real does not accept feature requests through the forums. You need to use their website form to do submit a feature request for considerations.

You are right, it's no feature request, just a discussion in the forum.
But I am sure they are smart enough to catch that from here for making their own thoughts and it should be part of their business.

However, you are right, if there is strong demand we should also submit one. :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: How to: Directly call an event handler (Undocumented?)
PostPosted: Fri May 29, 2009 2:23 am 
Offline
User avatar

Joined: Wed Nov 08, 2006 11:04 am
Posts: 135
Nate wrote:
Let's say you have a menu item "Buy" in the "File Menu". You've named it FileBuy and added a menu handler for it. You can call that menu handler like this:

Sub SomeOtherFunction()
' Uses undocumented functionality!

' Note the leading underscore: this is needed for calling
' menu handlers, but not control handlers.
_FileBuy_Action()
End Sub


Unfortunately this does not work with the RB Standard MenuItems like 'EditSelectAll'.
Probably their internal names look different from that.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group