472,119 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Not sure about this one

Is this the correct way to write this?

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
ds.Dispose();
}

Do I need to dispose it since I didn't explicitly create it? Can it hurt?

TIA,

Mike Rodriguez
Nov 17 '05 #1
7 973
Disposal of objects is normally up to the object that created it. If the method you are calling does not maintain a reference to
the DataSet, then you should dispose of it.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:u6**************@TK2MSFTNGP12.phx.gbl...
Is this the correct way to write this?

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
ds.Dispose();
}

Do I need to dispose it since I didn't explicitly create it? Can it hurt?

TIA,

Mike Rodriguez

Nov 17 '05 #2
This could should compile every time should run sometimes:)

I think u assumed that SomeFunctionThatReturnsADataSet will return a valid
dataset object everytime. It will never return null. If it is possible,
u should modify your code

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
if (ds!=null)
ds.Dispose();
}

And for your question, DataSet class derives from MarshalByValueComponent
which derives from IDisposable. At this point my advice u to modify your
code as given below;

using (DataSet ds =SomeFunctionThatReturnsADataSet() )
{
}

Every time, whenever program counter leaves using's scope compiler will
automatically call dispose method for u. It is not important if an exception
is thrown or how program counter leaves your function.

--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET

"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Is this the correct way to write this?

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
ds.Dispose();
}

Do I need to dispose it since I didn't explicitly create it? Can it hurt?

TIA,

Mike Rodriguez

Nov 17 '05 #3
Michael,

I would do it like this:

using (DataSet ds = SomeFunctionThatReturnsADataSet())
{
// Do some stuff with ds
}

This way, if an exception is thrown, then you don't have to worry about
it being disposed (and if the function throws an exception, the dataset will
not be returned).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Is this the correct way to write this?

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
ds.Dispose();
}

Do I need to dispose it since I didn't explicitly create it? Can it hurt?

TIA,

Mike Rodriguez

Nov 17 '05 #4
In answer to the question you posed at the end, the issue is not which
method explicitly created the object, but which method (if any) has
exclusive control of it when it "dies".

For example, if you put the DataSet in an ArrayList, the ArrayList
would then conceptually "own" that DataSet object, and the ArrayList
should Dispose of the DataSet when the ArrayList itself is no longer
needed (so you would need to subclass ArrayList in this case).

"Who owns what" is very important in object-oriented programming. It's
important to make the distinction between "I'm handing you a reference
to this object for your use, but the object is still mine," and "Here's
an object for you... I wash my hands of it... it's yours now."

Usually functions that return object references are doing the latter,
not the former.

Nov 17 '05 #5
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
In answer to the question you posed at the end, the issue is not which
method explicitly created the object, but which method (if any) has
exclusive control of it when it "dies".

"Who owns what" is very important in object-oriented programming. It's
important to make the distinction between "I'm handing you a reference
to this object for your use, but the object is still mine," and "Here's
an object for you... I wash my hands of it... it's yours now."


I think I understand now. So if my function looks like this:

public DataSet SomeFunctionThatReturnsADataSet()
{
DataSet ds1 = new DataSet();
// some code to fill the dataset here...
return ds1;
}

Then when I write:

DataSet ds = SomeFunctionThatReturnsADataSet();

ds now contains the reference to the dataset that got initialized in the
function. The function didn't dispose of it, so now it's up to the caller.

Thanks for the help!

Mike Rodriguez
Nov 17 '05 #6
Michael Rodriguez wrote:
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
In answer to the question you posed at the end, the issue is not which
method explicitly created the object, but which method (if any) has
exclusive control of it when it "dies".

"Who owns what" is very important in object-oriented programming. It's
important to make the distinction between "I'm handing you a reference
to this object for your use, but the object is still mine," and "Here's
an object for you... I wash my hands of it... it's yours now."

I think I understand now. So if my function looks like this:

public DataSet SomeFunctionThatReturnsADataSet()
{
DataSet ds1 = new DataSet();
// some code to fill the dataset here...

But as others have said, if there is an exception thrown here it will
bubble up to the catch/finally block and will try and dispose a null object.

seriously consider the using construct.

using(dataset ds = SomeFunc....())
{
}
return ds1;
}

Then when I write:

DataSet ds = SomeFunctionThatReturnsADataSet();

ds now contains the reference to the dataset that got initialized in the
function. Unless there was an exception before the initialised ds got returned.
The function didn't dispose of it, so now it's up to the caller. This is correct.

Thanks for the help!

Mike Rodriguez

Nov 17 '05 #7
"John B" <jb******@yahoo.com> wrote in message
news:42***********************@news.sunsite.dk...
Michael Rodriguez wrote:
I think I understand now. So if my function looks like this:

public DataSet SomeFunctionThatReturnsADataSet()
{
DataSet ds1 = new DataSet();
// some code to fill the dataset here...

But as others have said, if there is an exception thrown here it will
bubble up to the catch/finally block and will try and dispose a null
object.

seriously consider the using construct.

using(dataset ds = SomeFunc....())
{
}


Hi John,

You are of course correct. I think I'm going to go with the

if (ds != null)
ds.Dispose();

Thanks to all for the help,

Mike Rodriguez
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Oliver Spiesshofer | last post: by
11 posts views Thread by Hari Sekhon | last post: by
reply views Thread by takveen | last post: by
3 posts views Thread by sheldonlg | last post: by
reply views Thread by leo001 | last post: by

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.