473,382 Members | 1,400 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,382 software developers and data experts.

using() statement will it call dispose when return is called

LP
Hi,
Considering the following code:

using(mySQLconn)
{

if (someCondition==true)
return 0;
//more code after return
//do some db calls
}

return 1;
Will .mySQLconn.Dispose(); still be invoked even when return is called?
Thank you
Nov 16 '05 #1
3 6326
Actaully, Dispose should be called when the code block for USING ends, which
is before your "return" statement.

"LP" <lp@a.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
Considering the following code:

using(mySQLconn)
{

if (someCondition==true)
return 0;
//more code after return
//do some db calls
}

return 1;
Will .mySQLconn.Dispose(); still be invoked even when return is called?
Thank you

Nov 16 '05 #2
LP,
The short answer is "yes". The using statment is the same as:
try{
// do something with the connection
// maybe return
}finally{
mySQLconn.Dispose();
}

No matter what, Dispose() will be called. . . and because of the
implementation of the Dispose() method on SqlConnection, Close() is
called from within the Dispose() method.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #3

"Peter Rilling" <pe***@nospam.rilling.net> a écrit dans le message de news:
Oa**************@TK2MSFTNGP15.phx.gbl...
Actaully, Dispose should be called when the code block for USING ends,
which is before your "return" statement.
This is misleading. If your return statement is
return expression;
then the expression is evaluated "before" Dispose() gets invoked.

The big plus of "using" is that it guarantees that Dispose will be called
regardless of how the block terminates (after the last statement of the
block, via a return, break or continue, or via an exception). It is a short
cut for a try { ... } finally { resource.Dispose(); } as already mentioned.

Bruno.

"LP" <lp@a.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
Considering the following code:

using(mySQLconn)
{

if (someCondition==true)
return 0;
//more code after return
//do some db calls
}

return 1;
Will .mySQLconn.Dispose(); still be invoked even when return is called?
Thank you


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Cybertof | last post by:
Hi ! I'm wondering about the use of the 'using' statement. In a procedure, should i declare all instance variables in a using clause ? In the below code, what would happen if MyFont & MyFont2...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
5
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the...
14
by: Jonas | last post by:
Hi! I'm developing the middletiers of an ASP.NET application in VB.NET. I've got a business logic layer in which I would like to perform auditing to a database. Instead of making an auditing...
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
9
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully...
4
by: Jure Bogataj | last post by:
Hello! I'm using .NET 2005 (C#) and I've come across using statement. It seems ok, I was just wondering one thing. What if I returned object inside using statement using RETURN directive. Is...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.