如何从Visual FoxPro 9 OLEPUBLIC类返回一个数组?

时间:2023-02-04 07:29:07

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.

作为FoxPro的新手(但是在Clipper的旧手),我有点不知道如何从以下OLEPUBLIC类返回一个数组。编辑:我修改了代码,以考虑下面@Stuart的评论。

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.

鉴于以下VBScript,我可以打开文件。我似乎无法做的是找回比错误消息更有用的东西。

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

c:\ temp \ foo.vbs(3,1)sloop.db sloop.db:.getrecord p:\ testing \ sloop \ sloop.prg第41行错误语法错误。 200

Line 41, as it turns out, is

事实证明,第41行是

      RETURN @THIS.ada

which is really weird as that's the syntax that Microsoft suggests. Any clues?

这是非常奇怪的,因为这是微软建议的语法。有什么线索吗?

3 个解决方案

#1


Try 'return @ada', but VFP arrays have never played nicely with other languages despite attempts to make them do so.

尝试'返回@ada',但VFP阵列从来没有与其他语言很好地玩,尽管尝试让他们这样做。

#2


(5 years late, but perhaps still useful to someone out there...)

(晚了5年,但也许对那里的人有用......)

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.

更好的选择是让你的VFP代码改为创建一个Collection对象,然后解析数组并将所有元素添加到集合中(使用.Add()方法)。然后你可以将集合对象传递回VB,然后很高兴能够使用它。

#3


Your revised code works for me in VFP9SP2 - I had to build as an EXE but managed to access data from VBSCript.

您修改后的代码在VFP9SP2中适用于我 - 我必须构建为EXE,但设法从VBSCript访问数据。

This was my VBScript code:

这是我的VBScript代码:

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))

#1


Try 'return @ada', but VFP arrays have never played nicely with other languages despite attempts to make them do so.

尝试'返回@ada',但VFP阵列从来没有与其他语言很好地玩,尽管尝试让他们这样做。

#2


(5 years late, but perhaps still useful to someone out there...)

(晚了5年,但也许对那里的人有用......)

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.

更好的选择是让你的VFP代码改为创建一个Collection对象,然后解析数组并将所有元素添加到集合中(使用.Add()方法)。然后你可以将集合对象传递回VB,然后很高兴能够使用它。

#3


Your revised code works for me in VFP9SP2 - I had to build as an EXE but managed to access data from VBSCript.

您修改后的代码在VFP9SP2中适用于我 - 我必须构建为EXE,但设法从VBSCript访问数据。

This was my VBScript code:

这是我的VBScript代码:

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))