DataSet: passed by value, but behaves like by ref

  • Thread starter Thread starter Christian Ahrenkiel
  • Start date Start date
C

Christian Ahrenkiel

Hi,

I've got a DataSet (ds) which was created by a DataAdapter (da) method:

ds.Fill(ds).

This DataSet is passed through various methods (by value). Each method
changes some fields in the DataSet (for converting data in another format).
As this DataSet is _not_ class-wide declarated, each method returns the
changed DataSet and so it is passed through all methods and back to its
initial call.

Example:

private InititalCall()
{
DataSet ds;
// Filling DataSet via DataAdapter...

this.Method1(ds);

/* Although the returned DataSet is _not_ used, the DataSet (ds)
* shows all changes I did in Method1 and Method2!
* Normally I would have suspected, I would need an instruction like
*
* ds = this.Method1(ds)
*
* to apply the changes to my initial DataSet.
* Didn't I passed it by value? Why does it behave like by ref?
* Where is my fault? Is there a special behaviour I do not know yet?
*/
}

private DataSet Method1(DataSet ds)
{
// Some changes...
ds = this.Method2(ds);
return ds;
}

private DataSet Method2(DataSet ds)
{
// Some changes...
return ds;
}

Maybe anyone else is wondering...

Christian

P.S. The code sequence is just an example. I tried to symplify it, but I
am sure, in my real code sequence the DataSet is not class wide
declarated and not passed by ref.
 
Hi Christian,

It's the reference to the dataset that is passed by value not the dataset
itself.

DataSet ds1 = new DataSet();

DoSomethingWith(ds1);
....

private void DoSomethingWith(DataSet blrg)
{
// blrg is a completely new reference pointing to the same DataSet as ds1

TweakData(blrg);

// since blrg points to the same dataset as ds1 the changes will be seen
when using ds1 too;

blrg = new DataSet();

// now however blrg points to something new

ClearEverything(blrg);

// but ds1 still points to the old dataset and is not affected
}

If it was by reference ds1 would point to the new DataSet.


Happy coding!
Morten Wennevik [C# MVP]
 
Jon said:
I believe you're getting confused by what "pass by reference" and "pass
by value" mean, and what value types and reference types themselves
are.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

Thanks for this link. I always thought a DataSet parameter would create
a copy of the whole DataSet and not a copy of its reference. I wasn't
aware of 'reference types'.

By the way... I know it is offtopic ;) , but I'm curious now and maybe
someone knows: Is Java behaving the same with parameters?

Christian
 
Christian Ahrenkiel said:
By the way... I know it is offtopic ;) , but I'm curious now and maybe
someone knows: Is Java behaving the same with parameters?

Yes, Java is exactly the same, except that *all* parameters are passed
by value in Java - there are no "ref" or "out" modifiers.
 
Jon said:
Yes, Java is exactly the same, except that *all* parameters are passed
by value in Java - there are no "ref" or "out" modifiers.

Thanks for your help!
Christian
 

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

Back
Top