Microsoft KB Archive/129868

= How to Pass a UDT to an OLE Automation Server in VB 4.0 =

Article ID: 129868

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q129868



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 </li> Choose Options from the Tools menu, and in the Project Options, select Sub Main as the Startup Form and OLE Server as the StartMode.</li> The OLE Automation server is now ready. Press the F5 key to run the program, and then minimize it.</li></ol>

Part Two: Create the OLE Automation Controller
<ol> Start a new instance of Visual Basic.</li> Start a new project in Visual Basic. Form1 is created by default.</li>  Add the following code to the General Declarations section of Form1: Private Type UDT age As Integer name As String End Type </li>  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 </li> 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.</li></ol>

Additional query words: 4.00 vb4win vb4all

Keywords: KB129868

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.