The information in this article applies to:
- Standard, Professional, and Enterprise Editions of Microsoft
Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
SUMMARY
This article shows by example how to pass a user-defined type (UDT) to an
OLE Automation server in Visual Basic version 4.0.
MORE INFORMATION
A UDT cannot be passed to a public member function of an OLE Automation
Server. This behavior is by design. However, it is possible to pass a UDT
to a private member function or to declare a private variable of a user
defined type within a class module. But because these are private, they
cannot be accessed directly by an OLE Automation Controller or Client.
Therefore, you need to pass each field of the UDT, element by element, or
create a class with public variables corresponding to each field of the
UDT.
Step-by-Step Example
There are two parts to this example - creating the OLE Automation
Server and creating the OLE Automation Controller.
Part One: Create the OLE Automation Server
- Start a new project in Visual Basic. Form1 is created by default.
- Choose Class Module from the Insert menu. Class1 is created by default.
- Add the following code to the General Declarations section of Class1:
Private Type UDT
age As Integer
name As String
End Type
Private ClassUDT As UDT
Public Sub GetUDTFields(age As Integer, name As String)
ClassUDT.age = age
ClassUDT.name = name
End Sub
Public Sub DispalyClassUDT()
MsgBox "Hi! My Name is " & ClassUDT.name & ". I am " & _
ClassUDT.age & " years old"
End Sub
- Set the following properties for Class1:
Properties Value
-------------------------------------
Instancing 2 - Creatable MultiUse
Name Class1
Public True
- Choose Module from the Insert menu. Module1 is created by default.
- Add the following code to the General Declarations section of Module1:
Sub main()
End Sub
- Choose Options from the Tools menu, and in the Project Options, select
Sub Main as the Startup Form and OLE Server as the StartMode.
- The OLE Automation server is now ready. Press the F5 key to run the
program, and then minimize it.
Part Two: Create the OLE Automation Controller
- Start a new instance of Visual Basic.
- Start a new project in Visual Basic. Form1 is created by default.
- Add the following code to the General Declarations section of Form1:
Private Type UDT
age As Integer
name As String
End Type
- Add the following code to the Form_Click event of Form1:
Dim x As Object
Dim MyUDT As UDT
MyUDT.age = 25
MyUDT.name = "Joe"
Set x = CreateObject("Project1.Class1")
x.GetUDTFields MyUDT.age, MyUDT.name
x.DispalyClassUDT
- Press the F5 key to run the program. Click Form1. A message box will
display the fields of the UDT passed element-by-element to the OLE
Automation server.
|