Pass Out an Object Array in .NET
I'm writing a wrapper class to expose a subset of the functionality of a .NET FTP library edtftpne from Enterprise Distributed Technologies.
When you call the edtftp's GetFileInfos method you get an array of FTPFile objects returned. I'm able to iterate through these but I don't know how to pass them on as a new and different object array containing only Name and Size for each file. Here's the code I have. Sorry it's a little confusing because I have my own class named FTPFile and the .NET library I'm using also has a class named FTPFile. I'm using both of them here. I should probably change the name of my class just to avoid confusion:
Public Function GetFileList() As FTPFile() Implements IFTP.GetFileList
Dim ftpfiles() As EnterpriseDT.Net.Ftp.FTPFile
ftpfiles = fCon.GetFileInfos 'Fill object array
Dim f As EnterpriseDT.Net.Ftp.FTPFile
Dim t As FTPFile = New FTPFile 'My custom class to hold FileName and FileSize for Each file
For Each f In ftpfiles
'What do I do here to put these in my GetFileList array?
Next
End Function
I'm also baffled on how to write my own FTPFile class so that this function can assign properties to each object it creates but the external COM code will see the properties of my FTPFile class as ReadOnly.
Here's what my FTPFile class looks like:
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 ReadOnly Property FileName() As String Implements IFTPFile.FileName
Get
FileName = sFileName
End Get
'Set(ByVal value As String)
' sFileName = value
'End Set
End Property
Public ReadOnly Property FileSize() As Long Implements IFTPFile.FileSize
Get
FileSize = lFileSize
End Get
'Set(ByVal value As Long)
' lFileSize = value
'End Set
End Property
End Class
Maybe I'm going about this totally wrong. I'd just pass on the object array that I'm getting from the GetFileInfos method but COM clients won't have access to the class/object EnterpriseDT.Net.Ftp.FTPFile without me rewriting it, I'm assuming.
You have to create the array that you want to return, then you create objects for the array inside the loop:
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).FileName, ftpfiles(i).FileSize)
Next
Return result
End Function
Add a constructor to the FTPFile
class so that you can set the properties when you create each instance:
Public Sub New(name As String, size as Long)
sFileName = name
lFileSize = size
End Sub
Note: It's not recommended to use hungarian notation to specify data type in variable names. VB is a typed language, so it's not so important to keep track of the data types that you have to clutter up the variable names with it.
精彩评论