I am new to VB. I read online that in order to return from a function, you do something as follows -
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
My question is, does this syntax also work for user defined types? If not, what is the syntax. I tried the following -
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
But this gives me an error on the above line - "object variable or with block variable not set".
What am I doing wrong here?
I suppose clsDetails is not a UDT, but a class. For variables defined as objects you need to use the SET
keyword. I.e.:
set getDetails = details
For details using UDTs as function return values or parameters, see: User Defined Type (UDT) as parameter in public Sub in class module (VB6).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With