Normal Topic Scope of variables (Read 416 times)
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Scope of variables
Aug 20th, 2007 at 4:37pm
Print Post Print Post  
Hi, I've recently started progamming an app in Sesame and I have a question about the scope of user defined variables.

I've created a function that lives in the Global Code area. The function takes a String Array as a parameter.

I want to create that array just once, to cut down on repetition and increase speed.  But I get an error when I include the var declaration in the global code area. And if I declare it in an element's 'OnFormEntry' section, no other elements can see the array, which means I've have to declare it and populate it all over again.

Is there a way to declare a variable that all elements can use without making it a Global Static variable?

For example,  in Global Code:
Code
Select All
var orderelements as String
orderelements = @StringArrayElementList()

function calculate_total(orderelements as String) as double

return 0
end function
 



and then elsewhere in an 'OnFormChange' area of an element I would call
total = calculate_total(orderelements)

this is just a general example, but I hope you get the gist.

Thanks
Derrick
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Scope of variables
Reply #1 - Aug 20th, 2007 at 5:26pm
Print Post Print Post  
Hi Derrick,

1. When declaring a variable in GLOBAL CODE, use stat instead of var.

2. Your assignment of orderelements needs to be after the Function declaration.

3. Using @StringArrayElement List may not be reliable in GLOBAL CODE as GLOBAL CODE runs before the form is available.

4. By using StringArrayElementList, you will sum all the values on your form, including the total element itself. You may need to adjust your logic to accommodate this.

To make sure you only do this once, you can use On Form entry, but set a flag indicating that you have done this already. Something like this:

GLOBAL CODE
Code
Select All
stat sDone as Int
stat sOrderElements as String

function calculate_total(orderelements as String) as double

    return 0

end function

    sDone = 0 



ON FORM ENTRY
Code
Select All
If sDone = 0
{
    sOrderElements = @StringArrayElementList()
    sDone = 1
} 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Re: Scope of variables
Reply #2 - Aug 20th, 2007 at 5:55pm
Print Post Print Post  
Thanks alot. The Stat command is what I was missing out on. But since I'm so impatient, I went ahead and declared a Global Static Variable in OnFormEntry. It seems to works okay, but I'll go back and try it your way.


Derrick

  
Back to top
 
IP Logged