Microsoft KB Archive/138065

= How To Enforce Initialization of a VB OLE Class Object =

Article ID: 138065

Article Last Modified on 7/1/2004

-

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 Q138065



SUMMARY
This article suggests a way to enforce the proper initialization of a Visual Basic OLE object that is instantiated from a class module. The class module has an Initialize event that is invoked each time an object of that class is instantiated.

However, it is not possible to pass any parameters to the Initialize event of a class module. To implement an object constructor that takes parameters to properly initialize the object's members, you can define a Public method (called Create for example) in the class module and call it with parameters immediately after instantiating the object.



MORE INFORMATION
The following step-by-step example shows how to implement a Create Method that enforces proper object instantiation. Note that the following statement must be the first statement in each method or property procedure in the class module: If blnNotCreated Then Err.Raise vbObjectError, "MyServer", _ "Object Not Yet Created! Please Call the Create Method First" This will raise an error that is returned back to the client in case the Create Method is not called. Note also that the flag blnNotCreated is the only member that is set in the Initialize event. This is done to avoid the following action, which would result in the unnecessary overhead of the Not operator: If Not blnCreated then Err.Raise

Step-by-Step Example for Creating the Server
 Start a new project in Visual Basic. Insert a Class Module (Class1), and set its instancing property to 1 - Creatable SingleUse and its Public property to True.  Add the following code to the Class Module: Private intData As Integer Private strData As String Private blnNotCreated Public Function Create(param1 As Integer, param2 As _                  String) As Boolean blnNotCreated = False intData = param1 strData = param2 End Function Public Sub MyMethod If blnNotCreated Then Err.Raise vbObjectError, _ "MyServer", "Object Not Yet Created! Please Call _          the Create Method First" MsgBox strData End Sub Private Sub Class_Initialize blnNotCreated = True End Sub  On the Tools menu, click Options, then click the Project tab, and set the Project Name to MyServer. On the File menu, click Make EXE file to create an OLE server.

Step-by-Step Example for Creating the Client
 Start a new project in Visual Basic. Form1 is created by default. On the Tools menu, click References to add a reference to MyServer.</li>  Add the following code to the Form_Click event of Form1: Private Sub Form_Click Dim x As New MyServer.Class1 x.Create 7, "hello" x.MyMethod End Sub </li> Press the F5 key to run the program.</li></ol>

Keywords: kbhowto kbcode KB138065

-

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

© Microsoft Corporation. All rights reserved.