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

后端 未结 8 3256
刺人心
刺人心 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:27

    @Martin

    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.

    With/End With does not working like the Using statement, it doesn't "Dispose" at the end of the statement.

    With/End With works in VB 6 just like it does in VB.Net, it is basically a way to shortcut object properties/methods call. e.g.

    With aCustomer
      .FirstName = "John"
      .LastName = "Smith"
    End With
    
    0 讨论(0)
  • 2021-02-20 19:35

    One important point that hasn't yet been mentioned here is that setting an object reference to Nothing will cause the object's destructor to run (Class_Terminate if the class was written in VB) if there are no other references to the object (reference count is zero).

    In some cases, especially when using a RAII pattern, the termination code can execute code that can raise an error. I believe this is the case with some of the ADODB classes. Another example is a class that encapsulates file i/o - the code in Class_Terminate might attempt to flush and close the file if it's still open, which can raise an error.

    So it's important to be aware that setting an object reference to Nothing can raise an error, and deal with it accordingly (exactly how will depend on your application - for example you might ignore such errors by inserting "On Error Resume Next" just before "Set ... = Nothing").

    0 讨论(0)
提交回复
热议问题