使用.NET COM Interop DLL的对象数组时出错

时间:2011-03-28 16:56:56

标签: vb.net vba com-interop

我发现很多其他人都使用谷歌这个错误,但是我很难理解他们如何应用于我正在做的事情。

这是我在编译时得到的VBA错误:“标记为受限制的函数或接口,或者该函数使用Visual Basic中不支持的自动化类型。”

导致错误的VBA代码:

'ftp is also a com object created in code not posted here
Dim f() As wooxter.FTPFile
f = ftp.GetFileList 'Returns an object array of type FTPFile
Dim i As Integer
For i = 1 To (UBound(f) - 1)
    If fFileExists(stg.LocalPicDir & "\" & f(i).FileName) = True Then
        If fGetFileSize(stg.LocalPicDir & "\" & f(i).FileName) = f(i).FileSize Then
        'Error occurs on the above line at compile time
            'The error occurs specifically on f(i).FileSize, but not on f(i).FileName
            'fGetFileSize returns a VBA Long. f(i).FileSize is a VB.NET Long
    End If
    End If
Next

这是我的相关.NET代码:

Public Interface IFTPFile
    ReadOnly Property FileSize() As Long
    ReadOnly Property FileName() As String
End Interface

<ClassInterface(ClassInterfaceType.None)> _
Public Class FTPFile : Implements IFTPFile
    Private sFileName As String = ""
    Private lFileSize As Long

    Public Sub New(ByVal FName As String, ByVal FSize As Long)
        sFileName = FName
        lFileSize = FSize
    End Sub
End Class

'Fragment of a different class
Public Function GetFileList() As FTPFile() Implements IFTP.GetFileList
    Dim ftpfiles() As EnterpriseDT.Net.Ftp.FTPFile
    ftpfiles = fCon.GetFileInfos
    Dim result(ftpfiles.Length - 1) As FTPFile
    For i As Integer = 0 To ftpfiles.Length - 1
        result(i) = New FTPFile(ftpfiles(i).Name, ftpfiles(i).Size)
    Next
    Return result
End Function

1 个答案:

答案 0 :(得分:1)

COM没有构造函数的概念。或者更重要的是,带有参数的构造函数。如果为类声明任何构造函数,则它必须包含无参数构造函数。客户端代码将始终使用的那个。您需要创建FileName和FileSize属性。

下一个问题是VBA没有64位整数数据类型。将FileSize属性从Long更改为,例如,Integer或Double。避免单一,它没有足够的有效数字来准确存储文件大小。它不能存储16777217。