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

using and return (what do they do under the sheets . . .)

Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset? Return is supposed to
stop execution immediately, correct?

Just wondering.

Robert
Nov 16 '05 #1
3 1402
In article <Or**************@TK2MSFTNGP11.phx.gbl>, rakkerspamisevil91
@hotmail.com says...
Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset?
Yes.
Return is supposed to
stop execution immediately, correct?


No, return is supposed to return a value from the function... :)

But yes, return "exits" the function, but when .NET handles the "exit"
it see the "try/finally" block that is generated with a "using"
statement and executes the finally block before returning the value
(which disposes of the dataset).

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 16 '05 #2
Ahh--that makes sense. Thanks for the answer.

Robert
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <Or**************@TK2MSFTNGP11.phx.gbl>, rakkerspamisevil91
@hotmail.com says...
Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset?


Yes.
Return is supposed to
stop execution immediately, correct?


No, return is supposed to return a value from the function... :)

But yes, return "exits" the function, but when .NET handles the "exit"
it see the "try/finally" block that is generated with a "using"
statement and executes the finally block before returning the value
(which disposes of the dataset).

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 16 '05 #3

"Robert May" <ra****************@hotmail.com> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset? Yes. Return is supposed to
stop execution immediately, correct?


Except for Finally blocks which are guaranteed to run. The using block is
just shorthand for

try
{
da.fill(ds)
...
return ds;
}
finally
{
ds.Dispose();
}
Here after the return statement the finally block is executed.

You shouldn't be Disposing your DataSets. They don't really need it.
DataSet inherits IDisposable from MarshalByValueComponent. This is all junk
required for dragging and dropping DataSets onto Forms in design view, and
just confuses the runtime behavior of the the DataSet. Disposing a DataSet
doesn't really do anyting, so in your code your function will return a
disposed, but perfectly useful DataSet.

David

Nov 16 '05 #4

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

Similar topics

9
by: Scott Beavers | last post by:
I'm trying to create a form in Excel to sort from the form and take the data to another worksheet. I am very new to this and any help would be appreciated. I have a value in a cell that will...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
9
by: [Yosi] | last post by:
Can I make an Excel file without having Excel in my PC ? I want to create Excel files from my C# application , then open those files later in another PC who have Excel installed . As we can open...
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...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
3
by: =?Utf-8?B?Q2hhZCBTY2hhcmY=?= | last post by:
I have a 3 page ASP.NET web application that is using an asp:Wizard control on the main page. There is no authentication (completely open) and all processesing is done on this single page. I have a...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
10
by: Ed Jay | last post by:
I do not want to load two style sheets for screen and print media. I'm having difficulty grasping the use of the @print statement, or its syntax. Would someone please provide a simple explanation....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.