以下代码似乎工作正常:
Private Sub frmCalendar_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If qCal IsNot Nothing Then qCal.Dispose()
End Sub
Visual Studio代码分析会生成警告:
"CA2213 Disposable fields should be disposed 'frmCalendar' contains field 'frmCalendar.qCal' that is of IDisposable type: 'clsCalendar'. Change the Dispose method on 'frmCalendar' to call Dispose or Close on this field."
是否有充分的理由不将Dispose调用留在FormClosing事件中?
答案 0 :(得分:2)
通常你会在表单的Dispose()方法中处理你的对象。这样可以确保只有在不再有对表单的引用时才会处理您的资源。你永远不知道将来有人会想要复活你的封闭表格,现在你已经处理了表格可能需要的资源。在你的情况下它可能无关紧要,但从一般意义上讲,总是将你的处理代码放在Dispose()方法中
答案 1 :(得分:0)
通常当你创建一个表单时,Visual Studio会为你生成Dispose函数,所以你不需要,例如在C#中你得到:
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}
#endregion
}