C#中的对象错误 [英] Object in c# error

查看:74
本文介绍了C#中的对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为自己不是一个没有经验的程序员,也不是一个聪明的程序员,但是这个小问题困扰了我.在底部概述情况时设想简单的程序.

该程序的简要说明:
1.打开主窗体时,它声明一个窗口",并在构造函数中放置一个新的"ObjectToTest"

2.打开新的模态表单后,它将分配给表单的对象分配给两个名为"OriginalObject"和"ModifiedObject"的变量

3.然后,通过在其列表中添加9来修改"ModifiedObject"

4.当模态形式返回并显示2个消息框时,它们都显示相同的值9.但是创建的源变量具有不同的值.




我的问题:为什么MessageBox没有显示正确的变量?:confused:
(我的猜测是这与指针有关,而我没有经验)



谢谢那些为我解决了这个问题的人:)
-------------------------------------------------- -
主要表格代码:

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:

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;
        }

    }

推荐答案

您已经猜到问题是窗口构造函数中的指针:

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

OriginalObject = ObjectToEdit;
ModifiedObject = ObjectToEdit;

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



OriginalObject和ModifiedObject都指向ObjectToEdit,这意味着修改ModifiedObject的更改也发生在OriginalObject上,因为它们都指向ObjectToEdit.

一种解决方法是使ObjectToEdit继承自ICloneable并重写Clone().
然后,将ctor更改为:



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.


嗨BuzzyTom,

在名为window的类的构造函数中,您正在将一个名为ObjectToEdit的对象分配给变量OriginalObject和ModifiedObject.
这意味着两个变量现在都包含对同一对象的引用,以解释您在消息框中看到的内容.

您在类顶部执行的实例化在构造函数中被覆盖,因此完全多余.除此之外,在类级别进行实例化也不是一个好习惯.最好将它们放入构造函数中.


干杯,

曼弗雷德(Manfred)
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


这篇关于C#中的对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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