When must I set a variable to “Nothing” in VB6?

后端 未结 8 3252
刺人心
刺人心 2021-02-20 19:00

In one of my VB6 forms, I create several other Form objects and store them in member variables.

Private m_frm1 as MyForm
Private m_frm2 as MyForm

// Later...
Se         


        
8条回答
  •  青春惊慌失措
    2021-02-20 19:26

    Setting a VB6 reference to Nothing, decreases the refecences count that VB has for that object. If and only if the count is zero, then the object will be destroyed.

    Don't think that just because you set to Nothing it will be "garbage collected" like in .NET

    VB6 uses a reference counter.

    You are encouraged to set to "Nothing" instanciated objects that make referece to C/C++ code and stuff like that. It's been a long time since I touched VB6, but I remember setting files and resources to nothing.

    In either case it won't hurt (if it was Nothing already), but that doesn't mean that the object will be destroyed.

    VB6 had a "With/End With" statement that worked "like" the Using() statement in C#.NET. And of course, the less global things you have, the better for you.

    Remember that, in either case, sometimes creating a large object is more expensive than keeping a reference alive and reusing it.

提交回复
热议问题