Click here to Skip to main content
15,909,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]
Posted
Updated 29-Jun-10 6:46am
v2

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.
 
Share this answer
 
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.
 
Share this answer
 
CommonsCF.Data messageHolder = new CommonsCF.Data();
messageHolder = k.tmp;
messageHolder.To = "!";

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