CreateObject Function

   
CreateObject Function

Class

Microsoft.VisualBasic.Interaction

Named Arguments

No

Syntax

   objectvariable   = CreateObject(   progid   [,   servername   ]) 
objectvariable (required; Object)

A variable to hold the reference to the instantiated object

progid (required; String)

The programmatic identifier (or ProgID) of the class of the object to create

servername (optional; String)

The name of the server on which the object resides

Return Value

A reference to a COM or ActiveX object

Description

Creates an instance of an OLE Automation (ActiveX) object.

Prior to calling the methods , functions, or properties of a COM or ActiveX object, you are required to create an instance of that object. Once an object is created, reference it in code using the object variable you defined.

Rules at a Glance

  • If your project does not include a reference to the object, you must declare the object variable type as Object; this allows the variable to reference any type of object.

  • If an instance of the ActiveX object is already running, CreateObject may start a new instance when it creates an object of the required type.

  • CreateObject can only be used to create instances of COM (or ActiveX) objects; it cannot be used to instantiate .NET components .

Example

The following routine defines a generic Object variable, as well as an Excel application object. It then uses the Timer function to compare the performance of the code fragment that uses late binding to instantiate the Excel application object with the one that uses early binding. (For a discussion of late and early binding, see the second item under Section .)

 Private Sub TestBinding(  ) Dim dblTime As Double Dim strMsg As String ' Calculate time for late binding dblTime = Timer(  ) Dim objExcelLate As Object objExcelLate = CreateObject("excel.application") objExcelLate = Nothing strMsg &= "Late Bound: " & Timer(  ) - dblTime strMsg &= vbCrLf ' Calculate time for early binding dblTime = Timer(  ) Dim objExcelEarly As Excel.Application objExcelEarly = Excel.Application objExcelEarly = Nothing strMsg &= "Early Bound: " & Timer(  ) - dblTime MsgBox (strMsg, vbOKOnly, "Late and Early Binding") End Sub 

Programming Tips and Gotchas

  • The ProgID is defined in the system registry and usually takes the form library.class or application.class .

  • The Object data type is the most generic of Visual Basic objects. When an object variable has been defined as type Object, CreateObject performs what is termed late binding . This means that, because the precise object type is unknown at compile time, the object cannot be bound into your program when it is compiled. Instead, this binding occurs only at runtime, when the program is run on the target system and the CreateObject function is executed. This need to determine the object type by referencing the relevant interfaces at runtime is time-consuming and results in poor performance. You can vastly improve this performance by utilizing early binding . Early binding necessitates adding a reference to the required object to your project.

  • The servername parameter permits the specification of the name of the server on which the ActiveX object is registered. This means that you could even specify different servers depending upon prevailing circumstances, as this short example demonstrates :

     Dim sMainServer As String Dim sBackUpServer As String sMainServer = "NTPROD1" sBackUpServer = "NTPROD2" If IsOnline(sMainServer) Then     CreateObject("Sales.Customer",sMainServer) Else     CreateObject("Sales.Customer",sBackUpServer) End If 
  • To use a current instance of an already running ActiveX object, use the GetObject function.

  • If an object is registered as a single-instance object i.e., an out-of-process ActiveX EXE only one instance of the object can be created. Regardless of the number of times CreateObject is executed, you will obtain a reference to the same instance of the object.

  • It is considered good programming practice (and often a necessary one) to tidy up after you have finished using an object by setting objectvariable to Nothing . This has the effect of freeing the memory taken up by the instance of the object, and, if there are no other "live" references to the object, shutting it down. For example:

     objectvariable = Nothing 

See Also

GetObject Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net