Paramcount caution

Jan Schenkel janschenkel at yahoo.com
Fri Dec 27 13:25:00 EST 2002


--- Jim Hurley <jhurley at infostations.com> wrote:
> Here is something I found confusing.
> 
> In its simplest form my problem is this:
> 
> I define a button with the script:
> 
> on mouseUp
>    put "3,4" into a
>    print1 a
>    print2 a
> end mouseUp
> 
> on print1 x,y
>    get the paramcount
>    if it is  1 then
>      put item 2 of x into y
>      put item 1 of  x into x
>    end if
>    put x &  return  after field 1
>    put y & return after field 1
> end print1
> 
> on print2 x,y
>    print1 x,y
> end print2
> 
> 
> I would have assumed that the two handlers "print1"
> and its synonym 
> "print2" would be identical since "print1" just
> calls "print2", but 
> they are not.  When "print2" calls "print1", it
> appears to pass two 
> parameters even though y is empty.
> 
> The result of "mouseUp" in field 1 is:
> 
> 3
> 4
> 3,4
> 
> I would have thought it would be
> 
> 3
> 4
> 3
> 4
> 
> The origin or this problem is that I was trying to
> students a choice 
> of synonym for an operation, and they could use
> either.  But that 
> will be a problem in handlers which count parameters
> in this way. I 
> get the feeling there is a subtle difference between
> "empty" and 
> "missing"???????
> 
> Jim Hurley

Hi Jim,

You mostly answered the question yourself: when you
call print2 it has only 1 parameter, which it places
into x. Since it can't find a 'y' parameter, it
assumes you want to create it and thus passes an empty
value as the second parameter to print1.
If you change the script of  handler print2 to:

on print2 x,y
  if the paramCount is 1 then
    print1 x
  else
    print1 x,y
  end if
end print2

Then you'll get the result you expected. The key thing
here is that when the engine sees you want to call
print1 with a second parameter that doesn't exist yet,
it will make it for you, and empty to boot.

Jan Schenkel.

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the use-livecode mailing list