dynamic properties


[ Zettels Traum ] [ search / suche ]

von dp am 27.Mai 98 um 04:13:55:

>Also, is it possible to have a object generate new properties after it's
>created?

Yes. Just think of an object's properties as a special type of property list.

Here's a parent script that contains sample methods for initializing and getting dynamic properties.


------------------ Begin Parent script ----------------

property pName


-- PUBLIC METHODS --

on new me
_Init me
return me
end new


on mGetValue me, theProp
set allProps = _GetAllProps(me)
repeat with curIndex = 1 to count(allProps)
set curProp = getPropAt(a, curIndex)
if curProp = theProp then
return getAProp(me, theProp)
end if
end repeat
return #NOT_FOUND
end mGetValue


on mGetProp me, theValue
-- put error checking here
set allProps = _GetAllProps(me)
repeat with curIndex = 1 to count(allProps)
set curValue = getAProp(me, getPropAt(me, curIndex))
if curValue = theValue then
return getPropAt(me, curIndex)
end if
end repeat
return #NOT_FOUND
end mGetProp


on mInitProp me, newProp, newValue
setAProp me, newProp, newValue
end mIniProp


on mShowProps me
set tempList = [:]
sort tempList
repeat with curPropNum = 1 to count(me)
set curProp = getPropAt(me, curPropNum)
set curVal = getAProp(me, curProp)
setAProp tempList, curProp, curVal
end repeat
set outString = the date && the time &RETURN& "me: " & me &RETURN
repeat with curPropNum = 1 to count(tempList)
set curProp = getPropAt(tempList, curPropNum)
set curVal = getAProp(tempList, curProp)
put curProp & ": " & curVal & RETURN after outString
end repeat
put outString
end mShowProps


-- PRIVATE METHODS --

on _Init me
set pName = "Duke"
end _Init


on _GetAllProps me
set tempList = [:]
repeat with curIndex = 1 to count(me)
set curProp = getPropAt(me, curIndex)
set curValue = getAProp(me, curProp)
setAProp tempList, curProp, curValue
end repeat
return tempList
end _GetAllProps

------------------- End Parent script -----------------


Cut and paste the above into a parent script named "test". Then from the message window type the following:

set a = new(script "test")

mShowProps a
-- "5/18/98 11:24 AM
me:
pName: Duke
"

mInitProp a, #pSurName, "Kahanamoku"

mShowProps a
-- "5/18/98 11:25 AM
me:
pName: Duke
pSurName: Kahanamoku
"

put mGetValue(a, #pName)
-- "Duke"

put mGetValue(a, #abc)
-- #NOT_FOUND

put mGetProp(a, #pSurName)
-- "Kahanamoku"

put mGetProp(a, #abc)
-- #NOT_FOUND


The above methods are bare bones and could (should) use some error checking against data types and duplicate property values. I leave that up to you.

I didn't show the correct use from the message window of the mGetProp method. It should be:

mShowProps a
-- "5/18/98 11:25 AM
me:
pName: Duke
pSurName: Kahanamoku
"

put mGetProp(a, "Kahanamoku")
-- #pSurName

put mGetProp(a, "abc")
-- #NOT_FOUND

Cheerio,

m i l e s
_________________________________________________________________________

m i l e s l i G h T w O o d



Dazu:























D. Plänitz