Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Create Object & New Function

Posted on 2002-04-26
4
141 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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