Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create Object & New Function

Posted on 2002-04-26
4
Medium Priority
?
147 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:
Mohammed Nasman earned 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

824 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