1 thank you, ira. thank you, carol. 2 my name is peter and i'm an alphaholic

35
1 Thank you, Ira. Thank you, Carol.

Upload: nathaniel-lindsey

Post on 05-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

1

Thank you, Ira.Thank you, Carol.

Page 2: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

2

My name is Peter and I'm an Alphaholic.

Page 3: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

3

Variable Scoping and Pointer Variables

Dr. Peter Wayne

Page 4: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

4

Variable scoping

6 levels of variable scoping:

1) local

2) session

3) global

4) addin

5) layout

6) hidden addin

Page 5: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

5

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.

Page 6: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

6

Scoping_1

x=1

x="alpha"

Page 7: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

7

Scoping_2 and Scoping_3

dim x as nx=1ui_msg_box("x is",str(x))script_play("scoping_3")

x="alpha" Scoping_3ui_msg_box("x is",x)

Scoping_2

Page 8: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

8

Script_play_local

dim x as nx=1ui_msg_box("x is",str(x))script_play_local("scoping_3")

x="alpha"ui_msg_box("x is",x)

Scoping_3

Page 9: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

9

Shared variablesdim shared x as nx=1ui_msg_box("x is",str(x))script_play("scoping_5")

dim shared x as cx="alpha"ui_msg_box("x is",x)

Scoping_5

Page 10: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

10

Delete statementdim shared x as nx=1ui_msg_box("x is",str(x))script_play("scoping_5")

'Scoping_5:delete xdim shared x as c ' this x is a completely new variablex="alpha"ui_msg_box("x is",x)

Page 11: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

11

Variable Namespace

x=5name="peter"l=local_variables() ' l is a pointer to the local variable namespace

? variables_enum(l)= xnamel

? typeof(l)= "P"? typeof(x)= "N"

Page 12: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

12

Pointer sub-elements, or properties, are equivalent to variables within a namespace

defined by the pointer.

dim ptr as pptr.name="cian"ptr.age=40ptr.date={3/21/02}

? properties_enum(ptr)= nameagedate? variables_enum(ptr)= nameagedate

There is a congruence between properties_enum() and variables_enum() for pointer variables.

Page 13: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

13

With..end with

with ptrage=42lastname="chambliss"

end with

? ptr.lastname= "chambliss"

Page 14: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

14

delete user ' make sure no 'user' variable exists' now, create a local variable, "user"dim user as cuser="peter"

' create pointers to the global and local namespacesdim g as pg=global_variables()dim l as pl=local_variables()

with gdim user as puser.sex="M"user.home="Bronx"user.name=l.user

end with

Mixing namespaces

Page 15: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

15

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))

Page 16: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

16

with g.user ui_msg_box("and the value of user.name in the global namespace",\ name)end with

Page 17: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

17

Accessing a form's layout-level variables

'account' and 'cust_name' are layout-level variables on the form:

Page 18: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

18

dim account as caccount="0900"name="bo peep"dim l as pl=local_variables()

Referring to a form's layout 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()

Page 19: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

19

Running the script

Page 20: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

20

dim account as caccount="0900"name="rabins"dim l as pl=local_variables()dim c as pf=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 caccount=account ' this will fail - ambiguous reference to "account"' next assignment will succeed since "name" exists only in 'local_variables() namespace and is therefore unambiguouscust_name=nameend with

f.resynch()f.show()f.activate()

Page 21: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

21

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=20new_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))

Page 22: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

22

increment_by_reference(george)ui_msg_box("after call by reference",\"george.age is "+str(george.age))

Page 23: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

23

dim person as pperson.name="leslie"person.sex="M"person.age=18

With pointers, one function can change several variables

function change_identity(ptr as p) as Vwith ptr

age=age+1sex=if(sex="M","F","M")

end withend function

Page 24: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

24

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)

Page 25: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

25

Property arrays: arrays of pointer variables

lecture_string=<<%str%selwyn+new features of alpha five v. 5peter+pointer variables and namespacesira+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)

Page 26: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

26

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)

Page 27: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

27

lectures.sort("D","name")ui_msg_box("alphabetically, last speaker is",lectures[1].name)

Page 28: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

28

prefix="c:\program files\a5v5\xbasiccheckbook\\"checks=table.open(prefix+"mycheckbook.dbf")nrecs=checks.records_get()checks.close()dim deposits[nrecs] as pdeposits.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)

Property array from table

Page 29: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

29

deposits.filter("amount<500",.t.)ui_msg_box("deposits of at least 500",\deposits.dump_properties("date:amount"))

Apply a filter to a property array

Page 30: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

30

Collections

family_list=<<%list%geoff,sonelizabeth,daughterellen,wifegreg,sonted,sonbingley,dogdarcy,dog%list%

dim family as ufamily.initialize("1,2","1","2",family_list)

Page 31: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

31

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"))

Page 32: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

32

User-Defined Types

type membername as cage as n address as csex as c dues_date as d

end type

' now, we can writedim aug_mbr as {member}aug_mbr.name="Barry"aug_mbr.address="Pennsylvania"aug_mbr.sex="M"

Page 33: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

33

aug_mbr_props=properties_enum(aug_mbr)

dim aug_mbr_desc as Ucode=*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"))

Page 34: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

34

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=0member.dues_date={}member.address=""member.sex=""

end function

Page 35: 1 Thank you, Ira. Thank you, Carol. 2 My name is Peter and I'm an Alphaholic

35

dim mbr as pmbr=member() ' creates a pointer with null valuesmbr.name="Barry"mbr.dues_date=date()mbr.address="Pennsylvania"

' now, examine the pointerdim mbr_desc as Ucode=*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"))