Multiply two values from combo list...

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Multiply two values from combo list...

Post by TodayIsTheDay » Mon Oct 12, 2009 12:16 am

I have two combo list boxes on a page. I'm trying to get the value from each and multiply them when I click a button.


Just to see if it will work I put this in the combo list code...


I put this code into the combo box

Code: Select all

on menuPick pItemName
   put pItemName into MyVariable
   answer MyVariable
   
   switch pItemName
      
   end switch
end menuPick
So far so good. I get the answer dialog with the number I've picked.

Then I put this in a button code:

Code: Select all

on mouseUp
answer MyVariable
end mouseUp
I get nothing.... I'm sure it's something simple but I've fooled with this for about an hour now...

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Post by mwieder » Mon Oct 12, 2009 3:16 am

Ah... you're stumbling your way into the object hierarchy. This is a good thing <g> and you're close to one of those "Aha!" moments:

Your "myVariable" variable has a local scope - it can't be seen outside of your combobox object. If you want it visible outside the combobox then there at least a couple of alternatives. Here's the first:

The combobox object is at the front of an object hierarchy. It will get messages first, and messages it doesn't handle are passed through the hierarchy to the card and the stack (this is a simplified view - actually there are other objects along the way that can intercept messages, but let's not complicate things at the moment). So let's put the process one level back, in the card script:

Code: Select all

-- put this in the card script

local sFirstVariable
local sSecondVariable

on StoreFirstVariable pVar
  put pVar into sFirstVariable
end StoreFirstVariable 

on StoreSecondVariable pVar
  put pVar into sSecondVariable
end StoreSecondVariable 

on MultiplyThemAndDisplayTheResult
  answer sFirstVariable * sSecondVariable
end MultiplyThemAndDisplayTheResult
and put this into the first combobox:

Code: Select all

on MenuPick pChoice
  StoreFirstVariable pChoice
end MenuPick
and do the same with the second combobox. Now both comboboxes have access to the routines in the card, and the card handlers do the heavy lifting. You'll probably want a button on your card with

Code: Select all

on mouseUp
  MultiplyThemAndDisplayTheResult
end mouseUp
There are many ways to approach this situation, and this is just one. Richard Gaskin (da man) has a great writeup on the object hierarchy on his web site. I don't have the url in front of me at the moment (it really should be engraved on my brain by now), but poke around at www.fourthworld.com. The idea is that code should be placed in the appropriate place in the hierarchy, and "appropriate" is left up to you to decide - I usually start placing code down the hierarchy if I use a routine more that twice, but putting handlers in the right place enables all sorts of magic to happen.[/code]

Post Reply