swiftgp
swiftgp

Reputation: 1015

ShowDialog in .Net

I know that you should always call dispose on modal forms. However, I have a related question.

If I have a method in my application like

Private Sub tempMethod

Dim expForm as new ExplorerForm(tempDataTable)
expForm.ShowDialog

'Should I call dispose here?
'or would the form be automatically disposed when it goes out of scope
'after this method exits?

End Sub

Upvotes: 3

Views: 1710

Answers (1)

Chris Dunaway
Chris Dunaway

Reputation: 11216

When the form goes out of scope, it will be garbage collected at some point in the future and Dispose will be called then, but it's better to use the Using keyword in this instance:

Private Sub tempMethod
    Using expForm As New ExplorerForm(tempDataTable)
        expForm.ShowDialog()

        'Other code here
    End Using      'Form disposed here
End Sub

Upvotes: 3

Related Questions