Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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 | :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 Smile | :)
---------------------------------------------------
Main Form Code:
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):
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:
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 26-Dec-10 8:57am
Comments
SAKryukov at 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!
buzzytom at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
dmageiras at 26-Dec-10 15:25pm
   
Thanks for accepting my answer.
buzzytom at 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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 469
1 CHill60 465
2 OriginalGriff 325
3 PIEBALDconsult 285
4 BillWoodruff 193


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 26 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100