I have following code with three CA2000 warnings. For cellFirst variable, I could overcome this by using a “using” block. But for other two headercells, the controls are getting created in a helper function.
CA2000 : Microsoft.Reliability : In method GetTableCell(string, int, string, string), object 'lnkHide' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'lnkHide' before all references to it are out of scope.
CA2000 : Microsoft.Reliability : In method GetTableCell(string, int, string, string), object 'ltlText' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'ltlText' before all references to it are out of scope.
CA2000 : Microsoft.Reliability : In method GetTableCell(string, int, string, string), object 'newCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'newCell' before all references to it are out of scope.
CODE
protected void grdTransactions_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e != null)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = null;
try
{
newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
using (TableHeaderCell cellFirst = new TableHeaderCell())
{
cellFirst.ColumnSpan = 1;
cellFirst.Text = "FIRST";
newHeaderRow.Cells.Add(cellFirst);
}
//Associate
#region Associate
TableHeaderCell cellAssociate = GetTableCell("tableColGroupAssociate", 4, "associateHide", "Associate Transaction Info");
#endregion
//Financial
#region Financial
TableHeaderCell cellFinancial = GetTableCell("tableColGroupTransaction", 5, "financialHide", "Financial Transaction Info");
#endregion
newHeaderRow.Cells.Add(cellAssociate);
newHeaderRow.Cells.Add(cellFinancial);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
finally
{
if (newHeaderRow != null)
{
newHeaderRow.Dispose();
newHeaderRow = null;
}
}
}
}
}
private static TableHeaderCell GetTableCell(string cssClassName, int colSpan, string hideClassName, string displayName)
{
TableHeaderCell newCell = new TableHeaderCell();
newCell.ColumnSpan = colSpan;
newCell.CssClass = cssClassName;
LiteralControl ltlText = new LiteralControl();
ltlText.Text = displayName;
newCell.Controls.Add(ltlText);
HyperLink lnkHide = new HyperLink();
lnkHide.Text = SupportToolUIResource.HideLinkText;
lnkHide.CssClass = hideClassName;
lnkHide.Target = SupportToolUIResource.HideLinkTarget;
newCell.Controls.Add(lnkHide);
return newCell;
}
REFERENCE:
UPDATED CODE
Used using block to overcome warning
using (TableHeaderCell cellAssociate = new TableHeaderCell())
{
GetTableCell(cellAssociate,"tableColGroupAssociate", 4, "associateHide", "Associate Transaction Info");
newHeaderRow.Cells.Add(cellAssociate);
}
private static void GetTableCell(TableHeaderCell cellAssociate, string cssClassName, int colSpan, string hideClassName, string displayName)
{
cellAssociate.ColumnSpan = colSpan;
cellAssociate.CssClass = cssClassName;
using (LiteralControl ltlText = new LiteralControl())
{
ltlText.Text = displayName;
cellAssociate.Controls.Add(ltlText);
}
using (HyperLink lnkHide = new HyperLink())
{
lnkHide.Text = SupportToolUIResource.HideLinkText;
lnkHide.CssClass = hideClassName;
lnkHide.Target = SupportToolUIResource.HideLinkTarget;
cellAssociate.Controls.Add(lnkHide);
}
}
QUESTION
Is there any pitfall in the application "using" block (as shown in the updated code) to overcome the warning?