Uninitialized variables in C# and Dispose()

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
 
M

Miha Markic [MVP C#]

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?
 
G

Guest

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
 
M

Miha Markic [MVP C#]

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).
 
S

Stu Smith

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top