Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dim As New (VB6)

    I've heard that there is an issue with using

    Dim <var> as New <Class>

    in VB6, rather than

    Dim <var> as <Class>
    Set <var> = New <Class>

    There are certain instances in my code where Dim As New would be prefferable.

    Something to do with memory leaking/GPFing/BSOD or other mysterious runtime problem.
    Does anyone know if there is any substance to these claims?

    Many thanks,

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Dim As New (VB6)

    When you dim a variable as a New whatever, you have already allocated the resources for that variable, whether or not you ever actually get around to using it. If you wait until you need it, the resources aren't allocated until then. There were other issues, I believe, although I can't recall off-hand what they were.
    Charlotte

  4. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Dim As New (VB6)

    The published behaviour of Dim As New is that the object doesn't get created until the variable is called for the first time. Are you saying that even so, there is some kind of allocation that takes place, when declaration is made?

  5. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Dim As New (VB6)

    It might be better to say 'that the object doesn't get created until the variable is referenced for the first time', the difference being that once Dim As New is used the object in question can be instantiated implicitly and hence has the possibility of being invoked without the express need of the running code. It could for example be referenced in a subroutine which might, if the object was not available, take a different path. You cannot use a Is Nothing test because as soon as you do so, you reference and hence create an instance of the object, and so return False. It can be therefore difficult to debug code in a complex set of routines as you can never be sure if, when or why an object was created.

    Which approach to use depends I suppose on the nature and structure of the code.

    Andrew C

  6. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Dim As New (VB6)

    Thanks for jumping in, Andrew. I never was a fan to declaring objects as New whatevers instead of explicitly instantiating them, so my memory of the reasons had faded. <img src=/S/granny.gif border=0 alt=granny width=20 height=20>
    Charlotte

  7. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Dim As New (VB6)

    I concur with what you are saying completely. My terminology was slightly vaguer than yours, when I said 'called' I believe I was meaning exactly the same as you mean by 'referenced'. It is rare that I use Dim As New, for the same reasons you mention, however there are certain instances where I do use it. E.g. In a sub where I am 100% certain that a method or property of that object will be used, regardless of any other conditions and errors (maybe in the first line or two of that sub).

  8. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Dim As New (VB6)

    Adam,

    If you are absolutely certain that a the object will be referenced, I think it matters little which way it is instantiated. Actually I would have thought that probably the best reason for using Dim as New would be if you were unsure it would be referenced, and hence if it was not needed it would not consume any resources.

    As I do not have that much experience of using Dim As New, I cannot comment on GPFing etc.

    Andrew C

  9. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Dim As New (VB6)

    I would apply the reverse argument. I would say if you unsure if it is going to be used, then explicit instantiation should always be used. I would never use Dim as New if I am unsure if it is going to be used, since then I run the risk of an Is Nothing test failing, for instance. And besides, I religiously set all objects to Nothing before all exit points, I would then be instantiating unecessarily at that point, which was one of your earlier arguments against it.

Posting Permissions

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