By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Uninitialized variables in C# and Dispose()

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <j.*********@email.dk> wrote in message
news:55**********************************@microsof t.com...
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?
I come from VB .NET where the design above pose no problems, even with Option Strict turned on.
Any help is appreciated.

Regards,
Jakob

Nov 22 '05 #2

P: n/a
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
Nov 22 '05 #3

P: n/a
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" <j.*********@email.dk> wrote in message
news:74**********************************@microsof t.com...
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

Nov 22 '05 #4

P: n/a
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.

"Jakob Bengtsson" <j.*********@email.dk> wrote in message
news:2A**********************************@microsof t.com...
Thanks, your help is much appreciated.

Jakob

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.