How do I return an array from a Visual FoxPro 9 OLEPUBLIC class?
-
20-08-2019 - |
Question
As a newbie to FoxPro (but an old-hand at Clipper), I'm a bit at a loss to figure out how to return an array from the following OLEPUBLIC class. edit: I've modified the code belw to take into consideration the remarks made by @Stuart below.
DEFINE CLASS db AS CUSTOM OLEPUBLIC
DIMENSION ada(1) && public scope for later return
FUNCTION opendb( cpName )
SET MULTILOCKS ON
USE (cpName) EXCLUSIVE NOUPDATE
= CURSORSETPROP("Buffering",5)
RETURN ALIAS()
ENDFUNC
&& etc
FUNCTION getrecord( sAlias, nRecno )
SELECT (sAlias)
GOTO (nRecno)
fc = FCOUNT()
DIMENSION this.ada(fc)
FOR i = 1 TO fc
STORE CURVAL(FIELD(i)) to THIS.ada(i)
ENDFOR
RETURN @THIS.ada
ENDFUNC
ENDDEFINE
Given the following bit of VBScript, I can open the file fine. What I can't seem to do is get back anything more useful than an error message.
set sp = createobject("sloop.db")
al = sp.opendb("p:\testing\sloop\patient.dbf")
wscript.echo sp.getrecord(al,1)
This is the error message:
c:\temp\foo.vbs(3, 1) sloop.db sloop.db: .getrecord p:\testing\sloop\sloop.prg Error in line 41 Syntax error. 200
Line 41, as it turns out, is
RETURN @THIS.ada
which is really weird as that's the syntax that Microsoft suggests. Any clues?
Solution
Try 'return @ada', but VFP arrays have never played nicely with other languages despite attempts to make them do so.
OTHER TIPS
(5 years late, but perhaps still useful to someone out there...)
A better option would be to have your VFP code create a Collection object instead, then parse the array and add all the elements to the collection (using the .Add() method). Then you can pass the collection object back to VB, which is then happy to play with it.
Your revised code works for me in VFP9SP2 - I had to build as an EXE but managed to access data from VBSCript.
This was my VBScript code:
set sp = createobject("stack1.db") ' Different project name
al = sp.opendb("C:\WORK\VFP\DATABASES\DATA\DATA.DBF")
arrData = sp.getrecord(al,1)
msgbox(arrData(1))