English 中文(简体)
从 VB6 函数返回定义的用户类型
原标题:Returning User defined types from Functions in VB6
  • 时间:2012-05-23 14:50:21
  •  标签:
  • function
  • vb6

我是VB新来的 我在网上读到 为了从一个函数返回,你做一些事情如下:

Private Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
    Dim Res as integer
    Res = x + y
    Add = Res         use the function s name
End Function

我的问题是,这个语法是否也适用于用户定义的类型?如果不是,什么是语法。我尝试了以下的语法:

Public Function getDetails() As clsDetails

Dim details As clsDetails

Set details = New clsDetails

With details
   .X = "R"
   .Y = "N"
   .Z = "N"
     more code follows
End With

getDetails = details  gives error-> object variable or with block variable not set

End Function

但这让我在上行有一个错误—— “ 对象变量或块变量未设置 ” 。

我在这里做错什么了?

最佳回答

计算元件(cls Details)不是UDT,而是一个类。对于定义为对象的变量,您需要使用 SET 关键字。

set getDetails = details

使用UDTs作为函数返回值或参数的详情,见: