Solved

Create Object & New Function

Posted on 2002-04-26
4
139 Views
Last Modified: 2010-05-18
I used to develop my app. using VB6,COM,DCOM to create DLL file for my applicatoin .
  i want to know what is the different between

set Obj=CREATEOBJECT("Project.class") and
set Obj=NEW "Project.class"
also i want to know whitch is better?
Thans
Ahmed
0
Comment
Question by:ahmedali
4 Comments
 
LVL 22

Accepted Solution

by:
mnasman earned 100 total points
ID: 6972842
Hello

  Use New for early bind, and CreateObject to late binding

  and of course, early binding is faster
use new when u want to declare the variable as specific class ID,
but you for declaring the variable as object or variant use CreateObject

0
 
LVL 15

Expert Comment

by:ameba
ID: 6972967
(from book: "Effective Visual Basic")
"The New operator is a little bit faster than the CreateObject function because the client application doesn't need to resolve the ProgID to a CLSID at run-time. When you use the New operator, VB also provides a compile-time type check on the name of the class you're using to create the object. However, when you use the CreateObject function, VB cannot verify whether the ProgID is valid at compile-time because it's merely a string argument. This means you won't know whether you have a bad ProgID until you run your code.

On the other hand, the CreateObject function can offer a little more flexibility than New does. This is because you don't have to commit to a specific CLSID at compile-time. It's not that difficult to devise a design in which a client application dynamically chooses one ProgID from a set of compatible components at run-time. This is a great option when you'd like to design an application in terms of plug-compatible components."
----

Note that CreateObject works with early bound variables.

    Dim x As IPage     ' early bound variable
    If someCondition Then
        Set x = CreateObject("YourPlugIn.clsPage")
    Else
        Set x = CreateObject("YourPlugIn.clsNicePage")
    End If
    x.Show   ' early bound call (very fast)

and New works with late bound variables.

    Dim y As Object      ' late binding
    If someCondition Then
        Set y = New frmProducts
    Else
        Set y = New frmCustomers
    End If
    y.Show  ' late bound call (slow)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7670311
Hi ahmedali,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept mnasman's comment(s) as an answer.

ahmedali, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8092626
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now