360 likes | 572 Views
Thank you, Ira. Thank you, Carol. My name is Peter and I'm an Alphaholic. Variable Scoping and Pointer Variables. Dr. Peter Wayne. Variable scoping. 6 levels of variable scoping: 1) local 2) session 3) global 4) addin 5) layout 6) hidden addin. V ariable scope and lifetime.
E N D
Variable Scoping and Pointer Variables Dr. Peter Wayne
Variablescoping 6 levels of variable scoping: 1) local 2) session 3) global 4) addin 5) layout 6) hidden addin
Variable scope and lifetime Local variables exist for the lifetime of the script, and then are destroyed. Exception: script_play_local() preserves the local variables for the next script. All new variables are local unless explicitly declared otherwise. Global variables are accessible to all reports, forms, and scripts. Session variables exist for the lifetime of the "session", which usually means one form or report. Session variables can also be used to pass data from one field rule event to another.
Scoping_1 x=1 x="alpha"
Scoping_2 and Scoping_3 dim x as n x=1 ui_msg_box("x is",str(x)) script_play("scoping_3") Scoping_2 x="alpha" Scoping_3 ui_msg_box("x is",x)
Script_play_local dim x as n x=1 ui_msg_box("x is",str(x)) script_play_local("scoping_3") Scoping_3 x="alpha" ui_msg_box("x is",x)
Shared variables dim shared x as n x=1 ui_msg_box("x is",str(x)) script_play("scoping_5") Scoping_5 dim shared x as c x="alpha" ui_msg_box("x is",x)
Delete statement dim shared x as n x=1 ui_msg_box("x is",str(x)) script_play("scoping_5") 'Scoping_5: delete x dim shared x as c ' this x is a completely new variable x="alpha" ui_msg_box("x is",x)
Variable Namespace x=5 name="peter" l=local_variables() 'l is apointerto the local variable namespace ? variables_enum(l) = x name l ? typeof(l) = "P" ? typeof(x) = "N"
Pointer sub-elements, or properties, are equivalent to variables within a namespace defined by the pointer. dim ptr as p ptr.name="cian" ptr.age=40 ptr.date={3/21/02} There is a congruence between properties_enum() and variables_enum() for pointer variables. ? properties_enum(ptr) = name age date ? variables_enum(ptr) = name age date
With..end with with ptr age=42 lastname="chambliss" end with ? ptr.lastname = "chambliss"
Mixing namespaces delete user ' make sure no 'user' variable exists ' now, create a local variable, "user" dim user as c user="peter" ' create pointers to the global and local namespaces dim g as p g=global_variables() dim l as p l=local_variables() with g dim user as p user.sex="M" user.home="Bronx" user.name=l.user end with
user="ira" ' local variable 'user' ui_msg_box("the local variable, user, is now",user) ui_msg_box("the global pointer variable, user, \ has the following properties",properties_enum(g.user))
with g.user ui_msg_box("and the value of user.name in the global namespace",\ name) end with
Accessing a form's layout-level variables 'account' and 'cust_name' are layout-level variables on the form:
Referring to a form's layout variables dim account as c account="0900" name="bo peep" dim l as p l=local_variables() f=form.load("customer_entry") with customer_entry.variables() ' layout-level variables account=l.account cust_name=l.name end with f.resynch() f.show() f.activate()
dim account as c account="0900" name="rabins" dim l as p l=local_variables() dim c as p f=form.load("customer_entry") c=customer_entry.variables() Try not to use the same variable names in different namespaces. You can confuse Alpha Five (to say nothing of yourself)! with c account=account ' this will fail - ambiguous reference to "account" ' next assignment will succeed since "name" exists only in 'local_variables() namespace and is therefore unambiguous cust_name=name end with f.resynch() f.show() f.activate()
You can pass pointers to a function to achieve "call by reference" function increment_value as n (input as n) increment_value=input+1 end function function increment_by_reference as v(ptr as p) ptr.age=ptr.age+1 end function george.age=20 new_age=increment_value(george.age) ui_msg_box("after call by value","george.age is "+str(george.age)+\ crlf()+" and new_age is "+str(new_age))
increment_by_reference(george) ui_msg_box("after call by reference",\ "george.age is "+str(george.age))
With pointers, one function can change several variables function change_identity(ptr as p) as V with ptr age=age+1 sex=if(sex="M","F","M") end with end function dim person as p person.name="leslie" person.sex="M" person.age=18
ui_msg_box("leslie is initially",str(person.age)+" y.o. "+person.sex) change_identity(person) ui_msg_box("leslie is now", str(person.age)+" y.o. "+person.sex)
Property arrays: arrays of pointer variables lecture_string=<<%str% selwyn+new features of alpha five v. 5 peter+pointer variables and namespaces ira+user defined functions %str% dim lectures[3] as p lectures.initialize_properties("name+topic",lecture_string) speakers=lectures.dump_properties("name") ui_msg_box("speakers are",speakers)
found=lectures.find("user defined functions","topic") ui_msg_box("user defined functions is by",lectures[found].name) found=lectures.find("peter","name") ui_msg_box("peter is speaking on",lectures[found].topic)
lectures.sort("D","name") ui_msg_box("alphabetically, last speaker is",lectures[1].name)
Property array from table prefix="c:\program files\a5v5\xbasiccheckbook\\" checks=table.open(prefix+"mycheckbook.dbf") nrecs=checks.records_get() checks.close() dim deposits[nrecs] as p deposits.initialize_from_table(prefix+"mycheckbook.dbf",\ "type='D'","invert(amount)") check_dump=deposits.dump_properties("date:amount") ui_msg_box("deposits by descending value",check_dump)
Apply a filter to a property array deposits.filter("amount<500",.t.) ui_msg_box("deposits of at least 500",\ deposits.dump_properties("date:amount"))
Collections family_list=<<%list% geoff,son elizabeth,daughter ellen,wife greg,son ted,son bingley,dog darcy,dog %list% dim family as u family.initialize("1,2","1","2",family_list)
function truthval as c(inval as l) truthval=if(inval=.t.,"Yes","No") end function ui_msg_box("is selwyn in my family?",truthval(family.exist("selwyn"))) ui_msg_box("what relation is darcy?",family.get("darcy"))
User-Defined Types type member name as c age as n address as c sex as c dues_date as d end type ' now, we can write dim aug_mbr as {member} aug_mbr.name="Barry" aug_mbr.address="Pennsylvania" aug_mbr.sex="M"
aug_mbr_props=properties_enum(aug_mbr) dim aug_mbr_desc as U code=*for_each(tag,"aug_mbr_desc.set("+"\""+tag+"\",\ aug_mbr."+tag+")",aug_mbr_props) evaluate_template(code) ui_msg_box("aug_mbr's dump",\ aug_mbr_desc.dump("key"+chr(9)+"value"))
Alternative to user-defined type: 1) Define a function to initialize a pointer 2) Then create "instances" of that pointer type function member as p() member.name="" member.age=0 member.dues_date={} member.address="" member.sex="" end function
dim mbr as p mbr=member() ' creates a pointer with null values mbr.name="Barry" mbr.dues_date=date() mbr.address="Pennsylvania" ' now, examine the pointer dim mbr_desc as U code=*for_each(tag,"mbr_desc.set("+"\""+tag+"\",mbr."+tag+")",\ properties_enum(mbr)) evaluate_template(code) ui_msg_box("mbr's dump",mbr_desc.dump("key"+chr(9)+"value"))