Beeping Button Brouhaha
Expected an integer, got <a real number> evt.ex.fr.type;type.ref.frame -48406 (#6008D229).buttonClickScript(), 27: PushConstant NIL Entering break loop: level 1Hmm. This is the same location we broke at before. Last time the error was "
Expected an integer, got nil
". This time it got a real number. Let's take a look at beeps
:
GetNamedVar(0, 'beeps); #4418E79 2.00000Well, that's the problem. The
beeps
variable is a real number, not an integer. Using our brilliant deductive capabilities we realize that StringToNumber
must return a real number. Let's check within the Inspector:
StringToNumber("2"); #4419331 2.00000 StringToNumber("2.5"); #44193ED 2.50000We'll use the
Floor
function, which rounds down a real number to an integer, to fix our problem:
Floor(StringToNumber("2")); #8 2 Floor(StringToNumber("2.5")); #8 2Let's rewrite the
buttonClickScript
. We'd better keep in mind that StringToNumber
might return nil
if we pass in a nonnumeric string (wonder what would happen if we called Floor
with nil
?):
func() begin local beeps := StringToNumber(numBeeps.entryLine.text); if beeps then for i := 1 to Floor(beeps) do :SysBeep(); endWe rebuild, download, and rerun. We write in "2" in the input line and tap the Beep button. Lo and behold, the Newton beeps (although it's hard to tell there are two beeps because the second beep starts before the first beep finishes). Just to be thorough, we write in "two" in the input line and tap the Beep button. Nothing happens, just as we desire.
An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.
Last modified: 1 DEC 1996