HOWTO: Enforce Initialization of a VB OLE Class Object

Last reviewed: May 16, 1997
Article ID: Q138065
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 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

  1. 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.

  2. 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
    
    

  3. On the Tools menu, click Options, then click the Project tab, and set the Project Name to MyServer.

  4. On the File menu, click Make EXE file to create an OLE server.

Step-by-Step Example for Creating the Client

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. On the Tools menu, click References to add a reference to MyServer.

  3. 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
    
    

  4. Press the F5 key to run the program.


Keywords : IAPOLE kbcode vb4all vb4win kbhowto
Technology : kbole
Version : 4.0
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 16, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.