Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reason MyProc(@Array) doesn't work

Status
Not open for further replies.

SteveDingle

Programmer
Jul 26, 2004
254
GB
Heya All,

Having brain fart and/or going mad. I have a method which passes and array by reference to another method which populates the array. Psuedo code:

Code:
LOCAL ARRAY MyArray[1,1]
This.PopulateArray( @MyArray )
...process array

* PopulateArray
LPARAMETERS taMyArray
DIMENSION taMyArray[2,4]
...blah...blah

This type of stuff has worked for me for years. Now I'm up against a situation where the above breaks. Basically MyArray comes back as MyArray[1,1] as if nothing happened. I have traced it and the taMyArray is getting populated correctly its just when control is returned to the initial method.

The gosh darn mind numbing problem for me is that the deciding factor of whether it works or not depends on the "calling chain". IOW, the only difference I can find when this fails depends on how a method, 3 calls up the calling chain, receives an object reference.

FWIW, I know there are other way to do this, like using Collection object but this is old code and I don't want to have refactor tons of other code to deal with collections... just now

Thanks for any and all insight

Toodles,
Steve Dingle
D&S Business Solutions Ltd
 
Hi Steve

You cannot pass a LOCAL reference to anything! By definition it is local to the declaring procedure - remove the LOCAL fromthe declaration line and make it PRIVATE and then you will have no problem.

PRIVATE ARRAY MyArray[1,1]
This.PopulateArray( @MyArray )


----
Andy Kramek
Visual FoxPro MVP
 
Heya Andy,

Did try that and after reviewing the old code that did make sense but what I don't understand is that it's working some of the time. FWIW, I did write a little test prg and it didn't seem to make a diff. :-(

Code:
LOCAL ARRAY laTest[1,1]

DEBUGOUT TestArray( @laTest )
DEBUGOUT laTest[1,1]
DEBUGOUT laTest[1,2]

*--------------------------------------------------
*) Descript....: Populate passed array
*
*  Parameters..: taTest
*--------------------------------------------------
FUNCTION TestArray()
LPARAMETERS taTest

   LOCAL llTest as logical
   DIMENSION taTest[1,2]
   taTest[1,1] = "Hello"
   taTest[1,2] = "By"
   m.llTest = .T.

   RETURN m.llTest
ENDFUNC



Toodles,
Steve Dingle
D&S Business Solutions Ltd
 
Hello Mike,

>> That happened because I left out
>> the @ in the call to testb1. It's to

Thanks for the response but what you seemed to have missed is that my code, I mean the exact same method in the exact same object, works sometimes while other times it doesn't.

Just in case the following helps, let me try to describe the scenario from further up the calling chain.
Code:
loObj = CreateMyObj()

loObj.SomeMethod()
*Method SomeMethod
IF DecisionProperty = .T.
   loObj.MyMethod1()
ELSE
   loObj.MyMethod2()
ENDIF

* Method MyMethod1()
loObj.DoArrayprocess()
.... method 1 stuff

* Method MyMethod2()
loObj.DoArrayprocess()
.... method 2 stuff

* Method DoArrayProcess()
LOCAL ARRAY MyArray[1,1]
loObj.PopulateArray( @MyArray )

In the example above the the call to DoArrayProcess fails when called from MyMethod1, while the call from MyMethod2 works as expected.

As you can see the same method for the same object is being called. The only difference is that AFTER the DoArrayProcess is called, different things are done.

I have traced through and I can see the array which is passed to PopulateArray, being redimensioned and populated, both times.


Toodles,
Steve Dingle
D&S Business Solutions Ltd
 
Mike thanks again for the response.

>> I didn't miss that. I don't have an
>> answer why you're not seeing it work everytime,
>> yet.

My mistake since you had it in your response, I assumed you were suggesting I look for that.

>> Doesn't change the fact that the
>> advice you got was wrong.

Mistake are made Mike. I was not being mislead on purpose, think we need to get past that part.

>> You realize that unless the array is
>> declared before calling DoArrayProcess
>> it cannot be used after DoArrayProcess
>> by either Mymethod1 or Mymethod2?

Yes I do Mike. However the only difference between success or failure is when I call the method that calls the method from a different method... as in the pseudo code. Method1 and Method2 don't use the array at all however that seems to be the only difference I can find

>> BTW shouldn't you be using THIS.
>> instead of loObj for most of that? I

Pseudo code Mike, sorry if I wrote it poorly. I was just trying to show the "depth" of the calling chain and the fact that it is the exact same lines of code.

>> Can you demonstrate how to make the code below fail?

No I can't Mike. While I can appreciate all the time it took you to re-write my pseudo code, I can't ".... demonstrate how to make the code below fail?"

Believe me, before posting I will always try to get it down to the simplest of examples. I can't. I have been using this type of scenario throughout my framework, have been for years, never had it fail, randomly.

I was just hoping someone might say "Have you checked the ABC setting new to VFP 9 SP1" or something similar

FWIW,I have put this problem aside for now.

Thanks again for your help, if I figured out what caused it I will definitely post to this thread

 
Heya Tamar,

I never touch that but just for grin and giggles did a search through all the code, alas nothing.

Toodles,
Steve
 
Heya Mike,

No. Only SomeMethod calls MyMethod1 and MyMethod2 and as you can see they both call the same DoArrayProcess method. As for Decision, well of course something is changing that, that is why it's there. But nothing else references it

Toodles,
Steve Dingle
D&S Business Solutions Ltd
 
Okay Mike, will consider that. At the moment tho more concerned with figuring out my initial problem. Not quite up for a redesign of entire process. Baby steps
 
I did not read the whole discussion above for I am not good in english, but maybe you can use
EXTERNAL ARRAY ... in your function/method handling array?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top