Uninitialized variables in C# and Dispose()

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm trying to show a form as a dialog (using the .ShowDialog() method). After showing the dialog, I want to dispose of the form

I use this design (simplified to clarify the point)

MyDialog dlg
tr

dlg = new MyDialog()
dlg.ShowDialog(this)
if(dlg.DialogResult == DialogResult.Yes

// Process the selections in the dialog her

catch(Exception ex

// Handle the error her

finall

if(dlg!=null){dlg.Dispose();



The C# compiler refuses to compile this because of the uninitialized dlg variable in the finally block

Is there any other way to ensure that the dispsose method is called, regardless of any thrown errors

I come from VB .NET where the design above pose no problems, even with Option Strict turned on

Any help is appreciated

Regards
Jakob
 
Hi Jakob,

This would be the correct usage.
MyDialog dlg = new MyDialog();
try
{
dlg.ShowDialog(this);
if(dlg.DialogResult == DialogResult.Yes)
{
// Process the selections in the dialog here
}
catch(Exception ex)
{
// Handle the error here
}
finally
{
dlg.Dispose();
}
}

or as an alternative

using (MyDialog dlg = new MyDialog())
{
try
{
dlg.ShowDialog(this);
if(dlg.DialogResult == DialogResult.Yes)
{
// Process the selections in the dialog here
}
catch(Exception ex)
{
// Handle the error here
}
}

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Jakob Bengtsson said:
Hi,

I'm trying to show a form as a dialog (using the .ShowDialog() method).
After showing the dialog, I want to dispose of the form.
I use this design (simplified to clarify the point):

MyDialog dlg;
try
{
dlg = new MyDialog();
dlg.ShowDialog(this);
if(dlg.DialogResult == DialogResult.Yes)
{
// Process the selections in the dialog here
}
catch(Exception ex)
{
// Handle the error here
}
finally
{
if(dlg!=null){dlg.Dispose();}
}
}

The C# compiler refuses to compile this because of the uninitialized dlg variable in the finally block.

Is there any other way to ensure that the dispsose method is called,
regardless of any thrown errors?
 
Hi Miha

Thanks for your help, but as far as I can see your code risks an unhandled exception being thrown from the constructor of MyDialog!

I'd like to avoid taking that risk, and at the same time be able to dispose the MyDialog instance after use (if this instance has been initialized)

Regards
Jakob
 
Hi Jakob,

Constructor really shouldn't throw any exception at all.
However, if you are concerned about this
- and want to revert to your original code: set MyDialoge dlg = null;
- or add another try/catch wrapper around it one of my examples

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Jakob Bengtsson said:
Hi Miha,

Thanks for your help, but as far as I can see your code risks an unhandled
exception being thrown from the constructor of MyDialog!?
I'd like to avoid taking that risk, and at the same time be able to
dispose the MyDialog instance after use (if this instance has been
initialized).
 
If you are planning to throw an exception in your constructor, you should
ensure that the constructor itself disposes any unmanaged resources,
otherwise there is no way for the client to do so. The uninitialized
variable warning is a useful one; don't paper over potential errors by just
setting the reference to null.
 
Back
Top