Article ID: 185700
Article Last Modified on 3/10/2005
Project name: pjxUdtDll
Class name: clsUdtExample
Option Explicit
Public Type udtCustomer
CustName As String
CustID As Integer
End Type
Public Type udtEmployee
EmpName As String
EmpID As Integer
End Type
Public Sub PassUdtByRef(ByRef cust As udtCustomer)
'Receive the UDT as a specific data type.
Static i As Integer
i = i + 1
cust.CustName = StrReverse(cust.CustName)
cust.CustID = i
End Sub
Public Function ReturnCustUdt() As udtCustomer
'Return a UDT as a specific data type.
Dim cust As udtCustomer
cust.CustID = 99
cust.CustName = "Bill"
ReturnCustUdt = cust
End Function
Public Function WhichUdt(ByRef Guess As Variant)
'Get a UDT stored in a Variant and then check to
'see which UDT was passed.
If TypeOf Guess Is udtCustomer Then
MsgBox "you passed a Customer type"
ElseIf TypeOf Guess Is udtEmployee Then
MsgBox "you passed a Employee type"
Else
MsgBox "don't know what you passed"
End If
End Function
Public Sub PassUdtByVal(ByVal Emp As Variant)
'If you want to pass a UDT by value you must use
'use a Variant data type.
Static i As Integer
If TypeOf Emp Is udtEmployee Then
i = i + 1
Emp.EmpID = i
Emp.EmpName = StrReverse(Emp.EmpName)
MsgBox "Emp value in class: " & Str(Emp.EmpID) _
& " " & Emp.EmpName
Else
MsgBox "Unknow type"
End If
End Sub
Option Explicit
Dim obj As pjxUdtDll.clsUdtExample
Dim udtCust As pjxUdtDll.udtCustomer
Dim udtEmp As pjxUdtDll.udtEmployee
Private Sub Command1_Click()
'Pass the UDT by reference.
udtCust.CustID = 99
udtCust.CustName = "Bob"
obj.PassUdtByRef udtCust
MsgBox "Before: CustID = 99 CustName = Bob" _
& vbCrLf & "After: CustID = " & udtCust.CustID _
& " CustName = " & udtCust.CustName
End Sub
Private Sub Command2_Click()
'Get the value from the class.
udtCust = obj.ReturnCustUdt
MsgBox "CustID = " & udtCust.CustID _
& " CustName = " & udtCust.CustName
End Sub
Private Sub Command3_Click()
'Pass the type as a variant.
Dim vTemp As Variant
If Check1 Then
vTemp = udtCust
Else
vTemp = udtEmp
End If
obj.WhichUdt vTemp
End Sub
Private Sub Command4_Click()
'Pass the type in a variant by value.
Dim vTemp As Variant
udtEmp.EmpID = 99
udtEmp.EmpName = "Bill"
vTemp = udtEmp
'You can pass the Variant or the UDT to the method.
'Because the method takes a Variant as an argument
'it will copy the UDT into the Variant.
obj.PassUdtByVal udtEmp 'or obj.PassUdtByVal vTemp
MsgBox "Emp value in Form: " & Str(udtEmp.EmpID) _
& " " & udtEmp.EmpName
End Sub
Private Sub Form_Load()
Check1.Caption = "Pass udtCustomer to WhichUdt method"
Command1.Caption = "Pass UDT to Method"
Command2.Caption = "Receive UDT from Method"
Command3.Caption = "Pass UDT in Variant"
Command4.Caption = "Pass UDT in Variant ByVal"
Set obj = New pjxUdtDll.clsUdtExample
End Sub
184898
: PRB: Can't Use Late Binding When Server Method Uses UDT
186423
: HOWTO: Return and Assign Arrays with Visual Basic 6.0
187922
: PRB: Passing a UDT To Or From an ActiveX EXE may Fail on NT
Additional query words: kbActiveX kbDSupport kbdse kbVBp kbVBp600
Keywords: kbhowto KB185700