Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2007, 03:19 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default using() in a method that returns....

I've been given some code to maintain has this structure throughout:
Code:
public DataSet GetData()
{
    using(DataSet myData = 
        // local method that hits a database
        RunProcedure(
            "fetch_data", 
            new IDataParameter[]{}, 
            "dataType"))
    {
        return myData;
    }    
}
I'm relatively new to dotNet so I'm unsure, but I'm thinking that the using block is completely useless here. If the object were actually getting disposed, the method would not be useful. I'm betting that the object is not disposed, the local reference goes out of scope, and the returned object should then be disposed when appropriate.

Hopefully someone here can verify or refute my theory.

-Andy

 
Old April 12th, 2007, 04:41 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, your assumptions are correct. The method will successfully return the DataSet so there's no point in trying to dispose it in the method. Instead, you could do something like this if you want to dispose the DataSet explicitly:

public void SomeMethod()
{
    using(DataSet myData = GetData())
    {
        // work with the dataset here
    }
    // Here it will be disposed off
}

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ReadProcessMemory returns nothing evolution445 Visual Basic 2005 Basics 17 October 8th, 2008 07:56 AM
for-each carriage returns TiLaser XSLT 4 April 28th, 2008 07:31 PM
Not all code returns a value Doom C# 4 February 11th, 2008 03:48 PM
remove returns redruff Classic ASP Basics 13 January 9th, 2007 11:56 PM
How are Carriage Returns stored? SQLScott SQL Server 2000 3 November 24th, 2006 10:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.