在VB6中从函数返回自定义类型的用户定义类型

4

我是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

但是在上面的代码行上会出现错误 - "对象变量或 With 块变量未设置"。

我做错了什么?

2个回答

9

1
如果你的函数返回一个对象(类实例),那么你需要使用 SET 命令来赋值返回值。 - MicSim

0
// function definition  
Public Function add(a, b) 
    Dim c As integer
    c=Val(a) + Val(b) 
    add=c
End Function

// function calling 
    x=Text1.Text
    y=Text2.Text
    z=add(x, y) 
    MsgBox (z) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接