[VB6] Bug in VB zelf? *

Pagina: 1
Acties:

  • Kuhlie
  • Registratie: December 2002
  • Niet online
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Option Explicit

Private Sub Form_Load()
  Dim v1 As New Collection
  Set v1 = Nothing
  MsgBox (v1 Is Nothing)

  Dim v2 As Collection
  Set v2 = Nothing
  MsgBox (v2 Is Nothing)

  Dim v3 As Collection
  Set v3 = New Collection
  Set v3 = Nothing
  MsgBox (v3 Is Nothing)
End Sub


Je zou verwachten dat dit True, True, True geeft, maar dit is niet zo! De eerste geeft False! Dit lijkt een bug in VB... of zie ik dat verkeerd?

Oftewel: wat is het verschil tussen

code:
1
2
dim v as collection
set v = new collection

en
code:
1
dim v as new collection


Nu met mijn eigen klasse:

code:
1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit

Private Sub Form_Load()
Dim b As New Class1
MsgBox "voor alles"
MsgBox b Is Nothing
MsgBox "voor set nothing"
Set b = Nothing
MsgBox "na set nothing"
MsgBox b Is Nothing
MsgBox "na alles"
End Sub


code:
1
2
3
4
5
6
7
8
9
Option Explicit

Private Sub Class_Initialize()
MsgBox "init"
End Sub

Private Sub Class_Terminate()
MsgBox "terminate"
End Sub


krijg ik:

voor alles
init (blijkbaar 'late-binding': de klasse wordt pas ge-instantieerd op het moment dat je hem voor het eerst gebruikt)
False (logisch)
voor set nothing
terminate (logisch: ik beeindig de instantie)
na set nothing
init (hier gaat het onlogisch worden: zodra ik vraag of hij nothing is, wordt er weer een nieuwe instantie aangemaakt, terwijl ik dat niet wil!!!!)
false (dit wil ik dus ook niet)
na alles
terminate (hij gaat buiten z'n scope)

Aargh! Waarom? Is dit een bug in VB?


Nog even wat copy-past uit de Dim-help:
New, Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsicdata type, can't be used to declare instances of dependent objects, and can’t be used with WithEvents.
Dit zegt dus alleen dat hij wordt gemaakt bij eerste gebruik. Dat heb ik gemerkt. Maar er staat niet bij dat je hem niet kunt SetNothing-en...

Stukje verderop:
Also use a Dim statement to declare the object type of a variable. The following declares a variable for a new instance of a worksheet.

Dim X As New Worksheet

If the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.
Ook hier wordt niet gerept over het terug kunnen zetten op Nothing.

Verwijderd

Maar waarom zou je New gebruiken ?
If you instantiate it using New, then you can never check for nothing.
The reason being, that vb automatically populates the variable with
a new instance (if the variable is nothing) whenever you reference it.
Zie ook deze link:
http://groups.google.com/...thing%26sa%3DN%26tab%3Dwg

  • Kuhlie
  • Registratie: December 2002
  • Niet online
Aah... maar dat stond niet in de documentatie! Bedankt...

(en ik ben gouden regel nr 1 vergeten uit te voeren: google bezoeken)