473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Uninitialized variables in C# and Dispose()

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
4 3152
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: steve | last post by:
i've setup a include page that's responsible for building the basic layout of my web pages (header, menus, etc.). each page includes this "sysheader.php". the first page i've built with this...
4
by: Jakob Bengtsson | last post by:
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) ...
13
by: rswanster | last post by:
When I compile and run the following: #include <iostream> int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
12
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however,...
3
by: julien | last post by:
Hello, Is it possible if a boolean was initialized or not? For other types of variable, I usually check if it is null. But this not possible for a boolean. Thank you Julien
23
by: Tim Anderson | last post by:
Is this expected behavior? Winform with button and listbox: Dim i As Integer For i = 0 To 50 Dim s As String If i = 1 Then s = "Only once?" End If
2
by: Bert Szoghy | last post by:
Hello, I am missing something about Visual Basic .NET module variables and window close events. In the following code, after opening Form2 by clicking a button on Form1 and then closing...
21
by: sanjaymeher | last post by:
Hi, Right now addDynamicMemory(char **ptr, int size) method can able to handle if input ptr is intitialized to NULL or something. But how to improve this method to handle uninitialized pointed...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
18
by: Spoon | last post by:
Hello everyone, I suppose using uninitialized automatic integer variables leads to undefined behavior? i.e. int foo(void) { int bar; /* bar may be 0, or it may be non-0 */
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.