在循环中更改1个变量也会更改其他变量 [英] Changing 1 variable in Loop Changes the Other also

查看:70
本文介绍了在循环中更改1个变量也会更改其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是提供部分代码...在代码beloe中,当它到达messageHolder.To="!"时,它还会更改k.tmp.To的值

谁能告诉我为什么会这样以及如何解决这个问题?我需要k.tmp.To的值保持不变.




I''m just providing the partial code... In the code beloe when it reaches messageHolder.To="!", It also changes the value of k.tmp.To

Can anyone tell me why is this happening and how to remove this problem? I need the value of k.tmp.To to remain the same.




for (int i = 0; i < sharedData.pendingList.Count; i++)
{
    k = (sharedData.pendingMessages)sharedData.pendingList[i];

    if (k.tmp.To.StartsWith("@") || k.tmp.To.StartsWith("#"))
    {
           gpName = k.tmp.To.Substring(1); // the gpName without the @

           gpUserList = dbConn.getUserFromGroup(gpName);

           CommonsCF.Data messageHolder = new CommonsCF.Data();

           for (int i2 = 0; i2 < gpUserList.Count; i2++)
           {
                 messageHolder = k.tmp;

                 MessageBox.Show("Starts1: " + k.tmp.To.StartsWith("#").ToString());
                 if (k.tmp.To.StartsWith("#")) 
                 {
                       messageHolder.To = "!";



[修改:通常,在我们看到代码之前,我们喜欢看问题,以便我们知道我们是否可以提供帮助.首先看代码有时会使人们不去看它.此外,整理粘贴(例如,删除多余的空格)仅需花费几秒钟的时间,就可以使代码更容易阅读.]



[Modified: generally, before we see code, we like to see the question so that we know if we think we can help. Seeing code first can sometimes get people to not look at it. Also, just a few extra seconds of tidying up your pasting (ie. removing extra spaces) can make the code a lot easier to read]

推荐答案

等同于messageHolder到k.tmp,您只是在创建对同一对象的另一个引用.简而言之,messageHolder和k.tmp是同一事物的两个名称.

因此,每当您更改任何变量的任何属性时,两者都会更改.您需要创建该对象的深层副本以消除此问题.

要创建深层副本,您可以使用MemoryStream(如果该类可序列化)或Reflection创建一个新对象.
When you equate messageHolder to k.tmp, you are just creating another reference to same object. In simple words, messageHolder and k.tmp are two names of same thing.

Hence, whenever you change any property of any of the variables, both would change. You need to create a deep copy of the object in order to get rid of this.

To create deep copy, you can make use of MemoryStream (if the class is serializable) or Reflection to create a new object.


问题是您不处理值变量. (据我了解).当您复制一个值变量(即int i = j;)时,发生的事情是我实际上采用了该值.但是,对于复杂的变量,当您将一个变量设置为另一个变量时,它只是在内存中创建了一个指针.因此,如果k.tmp的地址为0x12928334,则当您设置messageHolder = k.tmp时,将messageHolder的地址设置为0x12928334.这意味着每当您在messageHolder中更改一个值时,k.tmp的值也将被更改.

如果要更改messageHolder而不是k.tmp,则需要克隆k.tmp. k.tmp可能具有Clone方法,或者您可能需要从头开始.这是我知道得到自己想要的东西的唯一方法.
The problem is that you aren''t dealing with value variables. (As far as I understand it). When you copy a value variable (ie. int i = j;) what happens is that i actually takes on that value. However, with complex variables, when you set a variable to another, it simply creates a pointer in memory. So, if k.tmp has an address of 0x12928334, then when you set messageHolder = k.tmp all that it does is set messageHolder''s address to 0x12928334. That means that whenever you change a value within messageHolder, k.tmp''s values will also be changed.

You need to clone k.tmp if you want to change messageHolder and not k.tmp. k.tmp might have a Clone method, or you may need to do it from scratch. That is the only way that I know of to get what you want.


CommonsCF.Data messageHolder = new CommonsCF.Data();
messageHolder = k.tmp;
messageHolder.To = "!";


我已将您的代码缩减以显示重要的行.
messageHolder是对CommonsCF.Data类实例的引用,因此ID为k.tmp.
当您将k.tmp分配给messageHolder时,它不会复制内容-它会复制引用.因此,当您修改messageHolder时,您还同时在修改k.tmp,因为它们都引用同一个实例.

就像手机一样,您可以将SIM卡从手机中取出,然后放入新手机中-该号码随SIM卡一起移动,因此当您拨打旧号码时,新电话会响铃.


I have cut your code down to show the important lines.
messageHolder is a reference to a CommonsCF.Data class instance, so id k.tmp.
When you assign k.tmp to messageHolder, it does not copy the content - it copies the reference. So when you modify messageHolder, you are also modifying k.tmp as they are both referring to the same instance.

It''s a bit like a mobile, you can take the SIM out of yours and put it in a new phone - the number moves with the SIM so the new phone rings when you call the old number.


这篇关于在循环中更改1个变量也会更改其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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