CreateObject Function


CreateObject Function

Class

Microsoft.VisualBasic.Interaction

Syntax

     Dim result As Object = CreateObject(progID[, serverName]) 


progID (required; String)

The programmatic class identifier (ProgID) of the object to create. The ProgID is defined in the system registry and usually takes the form library.class or application.class.


serverName (optional; String)

The name of the server on which the object resides. If omitted, the default value is an empty string (""), which indicates that the local server is to be used.

Description

The CreateObject function creates an instance of an ActiveX or COM server and returns a new object from that instance. Once created, that object's members can be accessed and used.

Usage at a Glance

  • If your project does not include a reference to the object's definition, you must declare the object variable type as Object.

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

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

  • This function does not support named arguments.

  • When using a variable of type System.Object to receive the result of the CreateObject function, the new object will be late bound. Late binding is inherently less robust in terms of performance than is early binding.

    The serverName parameter allows you to specify a remote system as the source of the new object instance. This code block demonstrates a possible use of this parameter:

         Dim primarySystem As String     Dim secondarySystem As String     Dim customer As Object     primarySystem = "MainServer"     secondarySystem = "BackupServer"     If IsOnline(primarySystem) Then        customer = CreateObject("Sales.Customer", primarySystem)     Else        customer = CreateObject("Sales.Customer", secondarySystem)     End If 

  • To obtain an object instance from an already running ActiveX server, use the GetObject function instead.

  • If an object is registered as a single-instance object (an out-of-process ActiveX EXE), only one instance of the object can be created at a time. Each time you call CreateObject to create this object, you will obtain a reference to the same instance of the object.

  • Always set the instance variable to Nothing when you are finished, so that all required cleanup code can run in a timely manner.

Example

The following code records the time required to access a Microsoft Excel application object, both by early-binding and late-binding methods.

     Public Sub TestBinding(  )        ' ----- Compare early and late binding.        Dim startingTime As Double        Dim message As String        Dim excelAppLate As Object        Dim excelAppEarly As Excel.Application        ' ----- Calculate time for late binding.        startingTime = DateAndTime.Timer        excelAppLate = CreateObject("Excel.Application")        excelAppLate = Nothing        message = "Late Bound: " & _           (DateAndTime.Timer - startingTime) & vbCrLf        ' ----- Calculate time for early binding.        startingTime = DateAndTime.Timer        excelAppEarly = Excel.Application        excelAppEarly = Nothing        message &= "Early Bound: " & _           (DateAndTime.Timer - startingTime)        MsgBox (message, MsgBoxStyle.OKOnly, "Late and Early Binding")     End Sub 

See Also

GetObject Function




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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