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