We have this base class that basically implements a collection (I know there are collections in VFP, but we can't change this overnight). Part of the process is to add an object to an array. This code word flawlessly on VFP6 for years.
this.ItemCount = this.ItemCount + 1
Dimension this.Item[this.ItemCount]
Local cItemPlace
cItemPlace = "Item[" + LTrim(Str(this.ItemCount)) + "]"
this.AddObject(cItemPlace, this.ItemClassName, &tInitParameters)
We are now preparing to upgrade our systems to VFP 9 and during the testing we get an "Object name is invalid" error on the line that adds the object. This error occurs very rare (maybe once every 10000 times that line is executed). If you retry it runs! One of the times I've seen a very weird thing in the debuger. The debuger properly showed the array containing objects and what appeared to be an object named "Item[30]" residing on the same object the array is sitting on (the code errored when it tried to add the 30th element). You can drill through the object in the debugger, but when you try to reference it as this.item[30], the element of the array is returned (instead of the rogue object).
Anyone has encountered this If I can't find an answer we will likely have to delay deploying VFP 9 in production. After buying 100 licences I know some executives will be ... ahem ... upset!
thanks
Sorin
Anyone ran into this

Bug in VFP 9? Adding an object to an array with AddObject.
SnowJim
From screen shot it looks like you were really using collection class. Is there a reason you're not using Collection.Add().
lary
Does it error if you instead code it as:
this.ItemCount = this.ItemCount + 1
Dimension this.Item[this.ItemCount]
this.Item[this.ItemCount] = ;
NewObject(this.ItemClassName, ClassLibNameHere, '', &tInitParameters)
Daddicool
It just happened again so here are more accurate details :
- When it errors, the array element where the object was supposed to be added remains .f. Say the line of code was :
this.addobject(cItem,cLib) where cItem was 'Item[20]'. The array element 20 will still be false but there is an object showing in the debugger on the "this" with the name "Item[20]". I have a screen shot of the debugger but I don't see any picture upload feature here. trying to reference this.item[20] will return .f., not the item[20] rogue object.
C Martin
local lcOldOnError, llError
lcOldonError = ON('Error')
on error llError = .T.
llError = .T.
do while llError
llError = .F.
&& add the object here
enddo
on error &lcOldOnError
Another approach:
local llError
llError = .T.
do while llError
llError = .F.
&& add the object here
if type('array_item_here') <> 'O'
llError = .T.
endif
enddo
You can also do while the array item type is not 'O'.
If you could post the relevant code of your custom class and code you are running to populate this array, we could probably come up with better solutions.
digitaladvisor
Dancer
LOCAL
oContainerFor
jx=1 To 10000oContainer =
Createobject('myContainer')Endfor
Define Class
myContainer As Container ItemCount = 0 Dimension Item[1] Procedure InitLocal
ix For ix=1 To 100 This.ItemCount = This.ItemCount + 1 Dimension This.Item[this.ItemCount] This.AddObject('Item['+Alltrim(Str(This.ItemCount))+']', 'Custom') EndforEndproc
Enddefine
skyfire
http://www.bostonphotographs.com/images/Picture4.jpg
I forgot to mention, so far retrying that line will run just fine.
thank you
Sorin
Mark Wilkins
Mark asked for some more details on how code is structured. CetinBasoz's myContainer example covers the basics pretty well. The this in Sorin's examples are an instance of a subclass derived from a "base_collection" class, which implements the fundamentals. There is no overridden AddObject method. This is the basic functionality derived from custom.
What makes this seems like a real bug in VFP(9 ) is the presence of an object member named this.item[1]. Look carefully at the debugger screen shot Sorin showed here:
this.item[1] is not an array element. And if if you try to refer to it in (e.g.) the command window, you get .F., which is the value of the 1st element in the item array, which is not the object member mysteriously named item[1].
Can any of you propose a way of legitmately creating/referring to/removing an object member named this.item[1], with square bracket in the name If so, we can scrutinize our code to see if we somehow could possibly be doing this (though it is doubtful, as this code is localized in a base class). But otherwise it seems like a VFP bug.
Cheers,
Richard
Chiwi
I don't know if it would error or not but that construct would not work in our code as NewObject or CreateObject will not create the object as a child to the "this" object and there are a milion places where code in one of the array element methods is referencing it's parent.
For example
_screen.AddProperty('aTest[2]')
_screen.atest[1] = NEWOBJECT('custom')
_screen.addObject('atest[2]','custom')
_screen.aTest[1].Parent.name && this will bomb because the object is not a child to the "screen" object.
_screen.aTest[2].Parent.name && this will work.
Alex Jankowski
I could not find any pattern between the instances where the errors occurs. It seems to be completely random. Someone (offline) suggested that it might be related to us using "Item" (a reserved word since VFP8) for the array name. You think this might have anything to do with it
thank you
Sorin
Strakian
marvino
"Foxpro gets confused about the object name unless you explicitly reference the containing object."
Try
cItemPlace = "This.Item[" + LTrim(Str(this.ItemCount)) + "]"
Stuart
Carrington2
this.ItemCount = this.ItemCount + 1
Dimension this.Item[this.ItemCount]
Local cItemPlace
cItemPlace = "Item[" + LTrim(Str(this.ItemCount)) + "]"
this.AddObject(cItemPlace, this.ItemClassName, &tInitParameters)
Does "THIS" refer to your custom class object If so, then your Custom class has an AddObject method with your own code that adds the object reference to the array property of the custom object. Is this correct If so, what does the code in your AddObject method look like Where is the code listed above located Is there any other code in that method that may help us if you posted it If you can help us create the minimum amount of code that you are using that generates this error, then we can run it thousands of times to see if we can get an error. Or at least see if there is a better solution.
RyanF