« Using the JInternalFrame Bean |
Main
| [Commentary] Filemaker 9: All New, All Improved, All Marketing Hype »
July 17, 2007
[Tip] Method as a function and as a method
by David Workman
Data Mosaic
Save some effort and have methods do double duty as both directly called methods or methods called by other methods. Just put a check in at the beginning of the code to see if the method was called by a UI object (you need to give the object a name) or if there is an argument being passed to the method (in which case you need to be passing an argument!).
Here's the basic idea by checking to see if a named object event is responsible for triggering the code:
if (application.getMethodTriggerElementName()) {
//method called from UI event
var input = application.getMethodTriggerElementName()
}
else {
//method called from another method so grab the arguments
var input = arguments[0]
}
Another example checking for a passed argument instead:
//set the tab panel name for this method
var tabPanelName = 'tabs_details';
//check to see if called from another method
if (!arguments[0])
{
var btn_name = application.getMethodTriggerElementName();
}
else
{
var btn_name = arguments[0];
}
//get number of tabs
var tab_num = elements[tabPanelName].getMaxTabIndex();
//activate correct tab and flip tab buttons
for ( var i = 1 ; i <= tab_num ; i++ )
{
var tab_name = 'tab_' + i;
if (btn_name == tab_name)
{
//elements[tab_name].fgcolor = '#FAAF0A';
elements[tab_name].bgcolor = '#ffffff'
elements[tab_name].setFont('Verdana,1,10');
//set tab index
elements[tabPanelName].tabIndex = i;
}
else
{
//elements[tab_name].fgcolor = '#000000';
elements[tab_name].bgcolor = '#999999'
elements[tab_name].setFont('Verdana,0,10');
}
}
| Posted by David Workman on July 17, 2007 at 10:53 AM in Tips | Permalink
Comments
Hi David,
Good tip but there is one thing though.
Functions are methods that return a result. None of the examples you gave do so they are not functions but methods (with and without arguments).
Posted by: Robert J.C. Ivens | Jul 18, 2007 9:29:09 AM
I am defining a function specifically as a method that can be called from many other methods to do a particular task (not confined to returning a data result). Passing and recieving data to the function is an optional thing.
In my examples I happened to pass information to the methods in the form of an argument or an object name. You could easily use the same check to return information to the calling method if that is what the situation called for.
Posted by: David Workman | Jul 18, 2007 10:10:35 AM
I guess you are right and it is my old VB (I know, I know) knowledge is peeking thru here. In VB there was a big distinction between a procedure and a function.
Posted by: Robert J.C. Ivens | Jul 18, 2007 5:28:37 PM
It was food for thought. Off the top of my head I can't think of a situation where I would return information based on where the "function" was called from. But the day that does happen (and it will), I'll remember this discussion!
Posted by: David Workman | Jul 19, 2007 10:02:33 AM