Collection
Hi all,
is it possible to assign a value to an item in a collection? The following
code produces an error, why?
loCol=CREATEOBJECT('collection')
loCol.Add('Item 1')
loCol.Add('.....')
loCol.Add('Item 3')
..
WAIT WINDOW loCol(2)
loCol(2) = 'Item 2'
Regards,
Henri
[376 byte] By [
Henri] at [2007-11-10 12:49:57]

# 1 Re: Collection
>loCol=CREATEOBJECT('collection')
>loCol.Add('Item 1')
>loCol.Add('.....')
>loCol.Add('Item 3')
>WAIT WINDOW loCol(2)
>loCol(2) = 'Item 2'
it would be more like "lcol2=locol.item2(2)". Where the property or function
returned the value of 2.You would be better of using an array in the example
that you entered. EX: loColarray(2)='item 2'.
# 2 Re: Collection
Henri,
Once added, collection objects themselves are read-only. If the collection object is an object
reference then you can change the values of the object's properties, but not the object itself. You
can add an Update method to your collection class that will handle the updates using intrinsic
Collection methods.
Ex.
loCol.Update('.....','Item 2')
define class mycollection as Collection
procedure Update
lparameters teKey, tuvalue
do case
case vartype(teKey) = 'C'
local lnpos
lnpos = THIS.GetKey(teKey)
if lnpos <> 0 then
THIS.Remove(teKey)
THIS.Add(tuvalue,,lnpos)
endif
case vartype(tekey) $ 'YNIB'
if teKey >= 1 and teKey <= THIS.Count then
THIS.Remove(teKey)
THIS.Add(tuvalue,,teKey)
endif
otherwise
* what were you thinking; don't do anything
endcase
endproc
enddefine
Regards.
--
Larry Miller
MCSD, Microsoft MVP Visual FoxPro
Bifrost Solutions
"Henri" <hvbalkom@pronexus.nl> wrote in message news:3f014f25$1@tnews.web.dev-archive.com...
>
> Hi all,
>
> is it possible to assign a value to an item in a collection? The following
> code produces an error, why?
>
>
> loCol=CREATEOBJECT('collection')
> loCol.Add('Item 1')
> loCol.Add('.....')
> loCol.Add('Item 3')
> .
>
> WAIT WINDOW loCol(2)
> loCol(2) = 'Item 2'
>
>
> Regards,
> Henri
>
>