当更改从源数据集复制的数据集值时,源数据集值将更改. [英] The source DataSet value is changing when changing a DataSet value which is copied from Source DataSet.

查看:73
本文介绍了当更改从源数据集复制的数据集值时,源数据集值将更改.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改从源数据集复制的数据集值时,源数据集值也在更改.

对于例如

The source DataSet value is changing when changing a DataSet value which is copied from Source DataSet.

For Eg

DataSet dataset1;
DataSet dataset2;

dataset2=dataset1;



//对数据集2进行一些更改
//更改也反映在dataset1中.



//Do some changes in dataset2
//the changes are reflecting in dataset1 also . How ?

推荐答案

好吧,您需要回到基础知识.

当声明一个类变量时,您不会创建该类的实例,而是会创建一个可以保存对该类实例的引用的变量.就像网页和http:地址之间的区别-后者是对前者的引用,但不是页面本身.

当您创建两个类变量并将一个变量分配给另一个变量时,仅复制引用(而不复制内容),它们都引用同一实例.再次从网页的角度来看,如果您有两个不同的站点,它们都具有指向同一网页的http:链接,那么无论您从哪个站点链接,都希望看到对该页面的任何更改.

因此,当您创建数据集时:
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:
DataSet dataset1 = new DataSet();
DataSet dataset2;
myDataAdapter.Fill(dataset1);
dataset2=dataset1;


对数据集1或数据集2的任何更改都反映在数据集的同一实例中-您只是通过不同的路线到达那里.

用图片更容易解释!



"Thanxx
但是..String也是引用类型.为什么字符串不发生
字符串str1 ="1";
字符串str2 = str1;
str2 ="2";
//str1的值保持不变,即"1" .."




这有两个原因:
1)在您给出的示例中确实发生了完全相同的事情:


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:

string str1="1";

创建引用str1 并为其分配字符串"1".

Creates a reference str1 and assigns a string "1" to it.

string str2=str1;

将对"1"字符串的引用从str1复制到str2.现在,两个变量都引用相同的字符串:"1"

Copies the reference to the "1" string from str1 to str2. Both variables now refer to the same string: "1"

str2="2";

创建新的字符串"2",并将引用分配给str2.这不会更改str1,它仍然指向字符串"1".

我告诉过你用图片更容易!


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:).


似乎在分配一个对另一个的引用只是告诉CLR,这两个引用都指向同一个对象,因此一个引用中的更改会反映另一个,因为它们正在写入相同的内存位置.

要复制u,可以使用
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
dataset2 = dataset1.Copy();

,它只是在不同的内存位置上显式复制值和结构.

希望这对您有帮助...

欢呼声

, which just does an explicit copy of the values and the structure on a different memory location.

Hope this helps...

Cheers


这仅仅是因为您要说出数据集2 =数据集1
,将数据集2复制到数据集1中

无论您编码了什么,都将得到完全相同的信息.
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.


这篇关于当更改从源数据集复制的数据集值时,源数据集值将更改.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆