Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The source DataSet value is changing when changing a DataSet value which is copied from Source DataSet.

For Eg

C#
DataSet dataset1;
DataSet dataset2;

dataset2=dataset1;


//Do some changes in dataset2
//the changes are reflecting in dataset1 also . How ?
Posted
Updated 16-Oct-11 22:52pm
v2

Ok, you need to go back to basics.

When you declare a class variable, you do not create an instance of that class, you create a variable that can hold a reference to an instance of that class. It's like the difference between a web page and a http: address - the later is a reference to the former, but it isn't the page itself.

When you create two class variables, and assign one to the other, the reference only is copied - not the content - they both refer to the same instance. Looking at in in terms of web pages again, if you have two different sites with an http: link to the same web page, then you would expect any changes to that page to be seen regardless of which site you linked from.

So, when you create a DataSet:
C#
DataSet dataset1 = new DataSet();
DataSet dataset2;
myDataAdapter.Fill(dataset1);
dataset2=dataset1;

Any changes to either dataset1 or dataset2 are reflected in the same instance of a dataset - you are just getting there by different routes.

This is a lot easier to explain with pictures!



"Thanxx
But..String is also a reference type. Why it is not happening for string
string str1="1";
string str2=str1;
str2="2";
//str1 value remains the same, ie "1".."




There are two reasons for this:
1) It does happen exactly the same in the example you give:
C#
string str1="1";
Creates a reference str1 and assigns a string "1" to it.
C#
string str2=str1;
Copies the reference to the "1" string from str1 to str2. Both variables now refer to the same string: "1"
C#
str2="2";
Creates a new string "2" and assigns the reference to str2. This does not change str1 which remains pointing at the string "1".

I told you this was easier with pictures!


2) Because strings are a special type of object - they are "immutable". What this means is that once a string is created, it cannot be changed. any method which appears to be changing a string content is in fact creating a new string and returning the reference to that - the original string remains untouched.
Don't worry about this yet - it won't affect you for a few weeks when you get a bit more advanced - the first explanation covers your example. I included this for completeness (and because someone would point it out if I didn't and I didn't want to confuse you :laugh:).
 
Share this answer
 
v2
Comments
vivektp 17-Oct-11 5:10am    
Thanxx
But..String is also a reference type. Why it is not happening for string
string str1="1";
string str2=str1;
str2="2";
//str1 value remains the same, ie "1"..
lukeer 17-Oct-11 5:32am    
That's because strings are immutable (cannot be changed after creation). They behave similar to value types. Every time you intend to change a string, there is a new one created internally and then your original reference set to that new one.

Edit: Oops, already answered in comment to Solution1
OriginalGriff 17-Oct-11 5:36am    
In this case, no! It is simply that he has overwritten one reference with another, leaving the original str1 value unchanged. :laugh:
OriginalGriff 17-Oct-11 5:35am    
Answer updated
I.explore.code 17-Oct-11 5:14am    
Nicely explained Mr.Griffin :) you are a GURU, my 5... I agree pictures make the explanation more convincing.
It seems like assigning one reference to the other merely just tells the CLR that both this references point to the same object so the changes in one reflect the other because they are writing to the same memory location.

To copy u can instead use
C#
dataset2 = dataset1.Copy();
, which just does an explicit copy of the values and the structure on a different memory location.

Hope this helps...

Cheers
 
Share this answer
 
v2
Comments
vivektp 17-Oct-11 5:05am    
String is also a reference type. Why it is not happening for string
string str1="1";
string str2=str1;
str2="2";
//str1 value remains the same, ie "1".
I.explore.code 17-Oct-11 5:12am    
String objects are immutable and they can also be treated as value types. So whenever you copy a string, it creates a new string in memory which is why you can retain both the values. This is one of the huge security issues as well where one is discouraged from using strings for password information, coz they can lurk around in memory for a while. StringBuilder is recommended instead, but i digress. The copying of all objects in C# works this way, they just create a new ref that points to the same memory location. Hope this helps
It's simply because you are copying the dataset 2 into dataset 1 by saying dataset2 = dataset1


Whatever you have coded, you are getting exactly the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900