Search notes:

VBA: Beware of dim … as new …

dim obj as new someClass can be used to declare and initialize an object-variable in one statement.
However, the use of dim obj as new class is a bit surprising, which I try to demonstrate with the following example:

X.cls

This source file defines a simple class:
option explicit

private value as long

private sub class_initialize() ' No parameters possible!
    debug.print "  class_initialize was called"
    value = 42
end sub

private sub class_terminate()
    debug.print "  class_terminate was called"
end sub

public sub doSomething() ' {
    value = value + 1
    debug.print("  value = " & value)
end sub ' }
Github repository about-VBA, path: /language/classes/constructor-destructor/beware-of-dim-as-new/X.cls

main.bas

This source file uses the class X. There are three loops wiht three iterations, respectively.
In the first two loops, the class is initialized in each iteration. In the last loop, however, the class is initialized only once.
In addition to that strange, imho, behaviour, the class is initialized when it is first referenced, not when the execution reaches the dim … as new … statement.
option explicit


sub main() ' {

    dim obj as new X

    debug.print("--------- start loops ----------")

    loop_1
    loop_2
    loop_3

    debug.print("--------- loops finished -------")

    debug.print("next statement: print y.value")
    obj.doSomething

end sub ' }

sub loop_1() ' {

    debug.print("--------- loop_1 ---------------")

    dim i as long
    for i = 1 to 3
        dim obj as X
        set obj = new X
        debug.print("  i = " & i)
        obj.doSomething
    next i

end sub ' }

sub loop_2() ' {
    debug.print("--------- loop_2 ---------------")

    dim i as long

    dim obj as X
    for i = 1 to 3
        debug.print("  i = " & i)
        set obj = new X
        obj.doSomething
    next i

end sub ' }

sub loop_3() ' {
    debug.print("--------- loop_3 ---------------")

    dim i as long

    for i = 1 to 3
        dim obj as new X
        debug.print("  i = " & i)
        obj.doSomething
    next i

end sub ' }
Github repository about-VBA, path: /language/classes/constructor-destructor/beware-of-dim-as-new/main.bas

Output

The example prints
main
--------- start loops ----------
--------- loop_1 ---------------
  class_initialize was called
  i = 1
  value = 43
  class_initialize was called
  class_terminate was called
  i = 2
  value = 43
  class_initialize was called
  class_terminate was called
  i = 3
  value = 43
  class_terminate was called
--------- loop_2 ---------------
  i = 1
  class_initialize was called
  value = 43
  i = 2
  class_initialize was called
  class_terminate was called
  value = 43
  i = 3
  class_initialize was called
  class_terminate was called
  value = 43
  class_terminate was called
--------- loop_3 ---------------
  i = 1
  class_initialize was called
  value = 43
  i = 2
  value = 44
  i = 3
  value = 45
  class_terminate was called
--------- loops finished -------
next statement: print y.value
  class_initialize was called
  value = 43
  class_terminate was called

See also

Classes/objects in VBA

Index