What is the difference between returning and getting a value from the parameters?

G

GG

Can somebody please explain the difference between these 2 methods and
why would anybody will be using the one vs the other?

1st getting DataSet from the method parameter
public static void FillDataset(DataSet dataSet, ...)
{
FillDataset(connection, null, commandType,
commandText, dataSet, tableNames, commandParameters);
}
Called like this
// DataSet that will hold the returned results
DataSet ds = new DataSet();
FillDataset(ds,...)


2nd getting dataset from return
public static DataSet FillDataset(...)
{
return FillDataset(connection, null, commandType,
commandText, dataSet, tableNames, commandParameters);
}
Called like this
// DataSet that will hold the returned results
DataSet ds = new DataSet();
ds=FillDataset(...)


Thanks
 
M

Morten Wennevik

Well, I would prefer the second since you immediatly see what the method
is "returning".
 
R

Ram [MSFT]

Its just another way of doing it. I would use the second method (Return) so
I can do something like this:

FillDataset.GetXML() or some other method.

Also with second method you don't have to create a varaibale to hold the
return value, and thus use less memory.
 
G

Guest

Just kidding but --> FYI approach #1 won't work since you didn't make the Dataset a ref or out param..

Both approaches will work fine but as .NET uses exception based error reporting many {probably most} people would say that Approach #2 is semantically cleaner because you can write code as

Dataset ds = FillDataset(...)

This code strongly implies that FillDataset will create and fill ds. If there is a problem filling ds then an exception will be thrown. So if fill returns then you know that it succeeded and that ds is ready for use..

--Richar
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi GG,

I agree with the other posters that the second method is more appropriate,
but not always.
Sometimes the caller have to create the object (because the callee simply
doesn't know how to do it or it might be able to create it but it will takes
too many parameters in the method prototype) In this case the method just
fills out the object.
The other situation when the first case is usefull is when the method has to
return error code and cannot throw exceptions (those are methods that can
easily fail and are likely to be used in loops)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top