Click here to Skip to main content
15,883,820 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I don't consider myself an inexperienced programmer nor a brilliant one however this little problem has stumped me. Envisage the simple program at the bottom outlining situation.

A brief explanation of the program:
1. When main form is opened it declares a 'window' and puts a new 'ObjectToTest' in the constructor

2. Upon opening the new modal form it assigns the object handed to the form to 2 variables named 'OriginalObject' and 'ModifiedObject'

3. I then modify 'ModifiedObject' by adding 9 to its list

4.When the modal form returns and shows the 2 MessageBoxs they both show the same value 9. Yet there source variable created is of different values.




My Question: Why is the MessageBox not showing the correct variable?:confused:
(My guess is this is something to do with pointers, something i'm not experienced with)



Thankyou to the people who clear this up for me :)
---------------------------------------------------
Main Form Code:
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string OriginalString = null;
            string ModifiedString = null;
            window TestWindow = new window(new ObjectToTest());
            TestWindow.ShowDialog(this);
            foreach (int OriginalNumber in TestWindow.GetOriginal().List)
            {
                OriginalString += " " + OriginalNumber;
            }
            foreach (int ModifiedNumber in TestWindow.GetModified().List)
            {
                ModifiedString += " " + ModifiedNumber;
            }
            MessageBox.Show("Original String = " + OriginalString);
            MessageBox.Show("Modified String = " + ModifiedString);
        }
    }





My own testing class (Simple class with one list property):
C#
public class ObjectToTest
    {
        private List<int> Something = new List<int>();
        public List<int> List
        {
            set
            {
                Something = value;
            }
            get
            {
                return Something;
            }
        }
    }



An empty form dialog box I created:
C#
public partial class window : Form
    {
        ObjectToTest OriginalObject = new ObjectToTest();
        ObjectToTest ModifiedObject = new ObjectToTest();

        public window()
        {
            InitializeComponent();
        }

        public window(ObjectToTest ObjectToEdit)
        {
            InitializeComponent();

            OriginalObject = ObjectToEdit;
            ModifiedObject = ObjectToEdit;

            //Make 'ModifiedObject' Different
            ModifiedObject.List.Add(9);
        }

        public ObjectToTest GetOriginal()
        {
            return OriginalObject;
        }

        public ObjectToTest GetModified()
        {
            return ModifiedObject;
        }

    }
Posted
Comments
Sergey Alexandrovich Kryukov 26-Dec-10 15:23pm    
Both answers (by dmageiras and Manfred R. Bihy) are correct and got my 5, despite of the overly trivial nature of the issue.

A note for you: there are not pointers in .NET (except in unsafe stuff). There are reference and value types, method parameter passing by reference or by value. The difference is not just terminology. No one can be possible "not experienced" with them, otherwise whole programming activity would be just useless.

I cannot see any practical sense in you code; so, if this is just to learn the background -- my respect. Good luck!
Thomas.D Williams 26-Dec-10 15:59pm    
Thank you for your comment. All useful information. I outlined that the program was a quick creation outlining my problem. My problem in hand is actually creating an undo function for a larger program by means of storing instances of an object in a list.

I am very new to actually using CodeProject and presumed this was the place for such matter. I shall have to Google ByRef and ByVal. I seem to remember them from my VB.Net days :p

As you have guessed the problem is pointers in window constructor:

OriginalObject = ObjectToEdit;
ModifiedObject = ObjectToEdit;

//Make 'ModifiedObject' Different
ModifiedObject.List.Add(9);


Both OriginalObject and ModifiedObject point to ObjectToEdit which means that modifying ModifiedObject changes occur to OriginalObject also since they both point to ObjectToEdit.

One soulution is to make ObjectToEdit inherit from ICloneable and override Clone().
Then, change ctor to:
OriginalObject = ObjectToEdit.Clone();
ModifiedObject = ObjectToEdit.Clone();


Thus, each variable will point to different objects and your message boxes will work fine.

Hope this helps.
 
Share this answer
 
Comments
Thomas.D Williams 26-Dec-10 15:15pm    
Thank you so much. Had me stumped for ages :p This worked perfectly and a very speedy reply. I think it will be researching into pointers :)
dmageiras 26-Dec-10 15:25pm    
Thanks for accepting my answer.
Hi BuzzyTom,

in the constructor of your class named window you're assigning one object named ObjectToEdit to both variables OriginalObject and ModifiedObject.
That means both variables now contain a reference to the same object explaining what you're seeing in the message boxes.

The instantiations you're doing at the top of the class are overwritten in the constructor and thereby completely superfluous. Besides that it's not good practice to do instantiations on the class level. It's better to place them into the constructor.


Cheers,

Manfred
 
Share this answer
 
v2

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