What is the reason for not instantiating an object at the time of declaration?

后端 未结 4 759
南旧
南旧 2020-12-01 16:09

I had to delve into some VB6 code recently and I saw this pattern all over the place:

dim o as obj
set o = new obj

Why not this?

         


        
4条回答
  •  旧巷少年郎
    2020-12-01 17:00

    Here's another caveat:

    For x = 1 to 100
       dim obj as new MyObject
    
       'Do something with obj
    Next
    

    You might expect that a new object is instantiated 100 times, but you'll find that it only gets instantiated the very first time. This one caught me off guard long ago.

    I still use this notation all the time though. Just make sure you know the behavior.

提交回复
热议问题