Results 1 to 12 of 12

Thread: CreateObject VS New

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    CreateObject VS New

    What's the difference between :

    VB Code:
    1. Set connTest = New ADODB.Connection

    and

    VB Code:
    1. Set connTest = CreateObject("ADODB.Connection")

  2. #2
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: CreateObject VS New

    CreateObject relys on the Registry, while New uses the object itself. Heard the New keyword is better for speed.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    Re: CreateObject VS New

    Thanks you!

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: CreateObject VS New

    Actually, in this case there is no difference. Using New is only beneficial over CreateObject when creating an instance of a class that is within the project.

    This KB article explains it pretty well

    How Object Creation Works in Visual Basic Components

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: CreateObject VS New

    Also, if you use the New style then its best to do it like so.
    VB Code:
    1. Dim connTest = ADODB.Connection
    2. Set conTest = New ADODB.Connection
    3.  
    4. 'Vs:
    5. Dim connTest = New ADODB.Connection
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: CreateObject VS New

    I assume you meant:
    VB Code:
    1. Dim connTest As ADODB.Connection '****
    2. Set conTest = New ADODB.Connection
    3.  
    4. 'Vs:
    5. Dim connTest = New ADODB.Connection
    ..for VB6

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: CreateObject VS New

    Umm, for the "New" style, not meaning .NET but the New keyword.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: CreateObject VS New

    RD - He was pointing out this "error" in your code:
    VB Code:
    1. Dim connTest = ADODB.Connection

    At any rate.... there are more significant differences between using "New" vs "CreateObject" and that's the issue of early vs. late binding. If you dim an object of a specific type, then use hte New keyword to create a new instance, you get early binding which is good for ensuring that the object types are correct and you get intellisense to boot. When using CreateObject, typicaly it's with a var dimed as Object, which results in late-binding, there's no guarantee that the resulting object is in fact the right type you are expecting. And since the IDE won't specificaly know the object type, no intellisense is available.

    It's possible to combine the two:
    VB Code:
    1. Dim rsMine as Adodb.Recordset
    2. Set rsMine = CreateObject("Adodb.Recordset")

    but since you'd have to set a reference to get the Dim to work anyways, might as well use the New keyword.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: CreateObject VS New

    Quote Originally Posted by RobDog888
    Dim connTest = New ADODB.Connection
    Isnt it suppose to be like this?

    VB Code:
    1. Dim connTest [B]As[/B] New ADODB.Connection

    And yap, CreateObject is used for Late Binding hence it might be slower (but you might not notice), it's commonly used in Server Side Scripts...

    VB Code:
    1. Dim WdObj as object
    2. ...
    3. Set WdObj =CreateObject("Word.Application")

    With the above code you gain the ability to work with different version of word.
    Last edited by dee-u; Jul 14th, 2005 at 01:15 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: CreateObject VS New

    Guilty as charged. That happens every now and then. Its from writting code in .NET and then going back to old VB6. New
    habbits are hard to break.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: CreateObject VS New

    The basic difference between New and CreateObject:

    Objects declared by New keyword are early bound.
    Objects declared by CreateObject are late bound.

    Just as Jacob Roman said New is faster than CreateObject for this reason.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: CreateObject VS New

    Using New or CreateObject has nothing to do with an object being Early or Late bound.

    Early or Late binding is determined by the variable Declaration only

    Dim X as Library.Class = Early Bound object
    Dim X as Object = Late Bound object

    CreateObject can be used to create a Late or Early bound object instance.
    New can only be used to create an Early bound object instance.

    New is only faster if the class is within the project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width