Function 从VB6中的函数返回用户定义的类型
我是VB新手。我在网上读到,为了从函数返回,您需要执行以下操作-Function 从VB6中的函数返回用户定义的类型,function,vb6,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 我的问题是,这种语法是否也适用于用户定义的类型?如果不是,语法是什么。我尝试了以下方法- P
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
但这在上面一行给了我一个错误——“对象变量或块变量未设置”
我做错了什么?我想CLSDeails不是UDT,而是一个类。对于定义为对象的变量,需要使用
SET
关键字。即:
set getDetails = details
有关使用UDT作为函数返回值或参数的详细信息,请参阅:。如果函数返回对象(类实例),则需要使用
SET
指定返回值。
// 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)