473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5323
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
4566
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
495
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
1621
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
7730
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
5944
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
25546
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
6185
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
870
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
3485
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
1778
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
7199
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
7074
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
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.