Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a DataTable from function 1

Status
Not open for further replies.

TiltingCode

Programmer
Apr 10, 2009
61
US
I have the below code:
Code:
public DataTable RSValues(string strSP, string strID, int intStatus, int intProcessID)
    {
      SqlDataAdapter DataAdapter = new SqlDataAdapter(strSP + " " + intProcessID + ", " + intStatus, strConnection);
      DataSet RS = new DataSet();
      DataAdapter.Fill(RS);
      System.Data.DataTable datTable = RS.Tables[0];

      return datTable;
    }

Is it okay that I'm returning a dataTable without destroying the object? I've been told that the garbage collector will take care of this but I want to write efficient code.

The above code will be used multiple times through out the application. If this isn't efficient can someone please help out on what I should do?

The dataset can consist of many records.

Thnx much
 
it's always good to call Dispose. I haven't dug into the data adapter code, but i would assume the Dispose is there to clean up connection/command objects that it created. In that sense i would dispose of the SqlDataAdapter within this function.
Code:
public DataTable RSValues(string strSP, string strID, int intStatus, int intProcessID)
{
   using(var adapter = new SqlDataAdapter(strSP + " " + intProcessID + ", " + intStatus, strConnection))
   {
      var set = new DataSet();
      adapter.Fill(set);
      return set.Tables[0];
   }
}

As for the DataTable. this function is a factory method. it creates objects. therefore it wouldn't make sense to dispose of the object within this function. have the calling code dispose of the data table instead, when it's finished using the datatable.
Code:
using(var data = RSValues(...))
{
   //pass data around to process.
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
jmeckley, I think you provided me exactly what I was looking for. Never heard of "factory method" but am currently reading about it and from what I found using the the "using(var adapter = new ...." causes the object to only be used inside the {} and then it destroys itself.

Thank you, thank you, thank you for your reply and help.
 
factory method" is a type of implementation of the factory pattern. the factory pattern is one of the "core patterns" defined by the Gang of Four (GoF) back in the 60's. (?)

There are a series of design patterns that form the basic building blocks of object oriented programming. Common patterns that you use every day are: state, factory, observer, iterator, command...

using is .net keyword. syntax sugar if you will. it only works with IDisposable. it's a very powerful interface. it's also an example of the command pattern.
Code:
using(IDisposable disposable = new ObjectThatImplementsIDisposable())
{
   disposable .DoSomething();
}
is syntax sugar for
Code:
IDisposable disposable = null;
try
{
   disposable = new ObjectThatImplementsIDisposable();
   disposable .DoSomething();
}
finally
{
   if(disposable != null)
   {
      disposable.Dispose();
   }
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top