473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

DataSet: passed by value, but behaves like by ref

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.
Nov 16 '05 #1
5 5319
Christian Ahrenkiel <ca********@gmx.de> wrote:

<snip>

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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]
Nov 16 '05 #3
Jon Skeet [C# MVP] wrote:
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
Nov 16 '05 #4
Christian Ahrenkiel <ca********@gmx.de> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] wrote:
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
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
5
by: Christian Ahrenkiel | last post by:
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...
2
by: Martin | last post by:
Hi! I'm new to ASP.NET WebApplications and wonder whether I've missed something out when programming it. I'm grateful for any hints to this problem! Thanks very much for your efforts! Martin...
1
by: | last post by:
Hi, I've defined an ObjectDataSource and a parameterized DataSet. I would like it if I could pass the parameter value that describes the query that creates the DataSet as part of a user control...
3
by: Bob Clegg | last post by:
I am in the same boat as Moondog on the 2nd July. I have a report designed against a dataset. Proved the datasset is OK by dropping a datagrid onto the form and it binds OK to the data. But the...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
7
by: Clamara | last post by:
When adding a new record from my form, I pre-set my DateTimePicker's value to System.DateTime.Today Since the "Today" value is used most of the time, the user doesn't need to select a date from...
0
by: Feng | last post by:
Hi, My VB.Net app uses Remoting to pass an extended DataSet object back to client from server. I extend the DataSet object by adding a "Message" property to it so that my server can insert a...
1
by: SomebodyElse | last post by:
Hi. Apologies if this has been asked here before - I've searched & searched but can't find anything. It's probably my serach parameters, but I'm having trouble even describing it to a search...
7
by: Bob | last post by:
Hi, I have a typed unbound dataset that is passed to a datahandling class to be filled. The datahandling class fills it from a sproc using an oledbDataAdapter (SQLAnywhere database) The only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.