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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Array to a function ?

Status
Not open for further replies.

engan

Programmer
Jul 15, 2001
53
ID
Hi all,

How do I pass an array to a function ?

here is my code :

LOCAL ARRAY myarray[15]
myudf(myArray)

FUNCTION myudf(myarray)
myarray[1] = 12
myarray[2] = 15
ENDFUNC

When I compile it , I got an error message. It says cannot find myarray.

Eng An
 
Mike, I already try passing by reference and still got the same error. The exact error message is :

Unable to find unknown myarray

If I delete the statement myarray[1] = 12 and myarray[2] = 15
the error message does not appear.

Eng An


 
Eng An,
This should work:

myUdf(@myArray)

FUNCTION myUdf
PARAMETERS anArray
anArray[1] = 12
anArray[2] = 15
ENDFUNC

Stella
 
Eng An,

I can't see why you are getting this error.

Perhaps you could try:

FUNCTION myudf
LPARAMETER MyArray
myarray[1] = 12
myarray[2] = 15
ENDFUNC

Alternatively, try executing SET UDFPARMS TO REFERENCE before you call the function, and SET UDFPARMS TO VALUE afterwards (although I don't think that will make a difference).

Mike


Mike Lewis
Edinburgh, Scotland
 
Stella, I try your suggestion and still got the same error.

Mike, I still got the same error even I add SET UDFPARMS TO REFERENCE

right know I use vfp 7.0 to compile my program.

Mike , did you get any error message when you compile on your machine ?

engan
 
Mike, I think the problem is on my prg files. I have 2 prg files. 1 for the calling program dan the other is the function I called. When I put together into one file, I got no error message.

Eng An

 
Hi !
try:
set library to NameOfPrgWithFunction

Regards

Monika from Warszawa (Poland)
(monikaiz@o2.pl)
 
engan,

In your first program, use this syntax:

DIMENSION myArray[1]
EXTERNAL ARRAY myArray

Do whatever here...
myUdf(@myArray)


In the procedure file:
FUNCTION myUdf
PARAMETERS anArray
anArray[1] = 12
anArray[2] = 15
ENDFUNC

The project manager may need to know to look elsewhere for the array or you may get an error.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Dave, I try your suggestion but it still give me an error when compiling.

Instead I changed the code and got no error message.

First Program
DIMENSION myArray[1]

Do whatever here...
myUdf(@myArray)

In the procedure file:
FUNCTION myUdf
PARAMETERS myArray
EXTERNAL ARRAY myArray
myArray[1] = 12
myArray[2] = 15
ENDFUNC

If we use external array inside the function, that means we cannot use different name for the parameter.

Eng An
 
Engan,

Try to make your array public, i encountered that before the solution i found is to make my array public then after that i release the variable.
 
remsor, I try my array public and it works find. The thing is if we have array public then we don't have to pass as a parameter. What I like to do is having local array then pass it to a function as a parameter.

Eng An
 
Eng An,

The error is just prompt by the foxpro builder but the code will work, if ur calling the function from form u can create array property of the form then pass this to ur udf.
 
Eng An,

A completely different approach might be to pass an object rather than an array. The object can have a custom array property.

I know that would be a little inconvenient, but it would solve the problem of calling by reference or making it public.

Mike


Mike Lewis
Edinburgh, Scotland
 
DSummZZZ had the right answer: but put the statement:
EXTERNAL ARRAY MyArray
inside the function that receives the array.


However, it does not carry the implication that you mention... this works fine:

Code:
DIMENSION MyArray[5]
MyArray[1] = 'whatever'

res = MyUDF( @MyArray )

PROCEDURE MyUDF
LPARAMETERS aParam1
EXTERNAL ARRAY aParam1
if aParam1[1]='whatever'
  RETURN 'good'
endif
RETURN 'bad'
ENDPROC

The whole purpose of "EXTERNAL ARRAY" is to let the project manager know that this identifier (aParam1) is not DIMENSIONED inside this function, but should be accepted as an array. Basically, it just tells the project manager, "Don't throw an 'unknown array' error while building/compiling".

 
I wholeheartedly agree with wgcs. There are times when the compiler cannot know something is declared or instantiated and it is just trying to be helpful. There are usually two solutions: Ignore the error during compile or let the compiler know that everything is ok such as using "external array". There are even some more insidious "fake" compile errors such as when using API functions that have been declared properly but the compiler is completely unaware of. In these cases it is sometimes necessary to create a dummy procedure file with functions having the same name as the API functions and make the compiler think that the functions exist, otherwise you are stuck with constantly ignoring the compile errors when compiling your app/dll/exe. Either way these types of errors have no effect on your running code, though I would strongly suggest getting rid of them and not just ignoring them for obvious reasons.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Slighthaze has some good points... when he says:
In these cases it is sometimes necessary to create a dummy procedure file with functions having the same name as the API functions and make the compiler think that the functions exist,

note that you must mark this file "Excluded" in the project manager, so that the stubs don't override the DECLARE'd API functions (I'm not sure if the UDF's WOULD override them, but I use this method for building 'plug-in' .APP's that call UDF's in the main 'host' program, and the .APP functions WOULD override them, if their containing .PRG weren't "excluded".)

FWIW: I use a RELEASE.PRG to do all the building of a project, and I include that PRG in the project, marked "Excluded", and put the stup PROC's in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top