将ref对象传递给多窗体应用程序的类构造函数 [英] Passing a ref object to a class constructor for a multiple forms application

查看:54
本文介绍了将ref对象传递给多窗体应用程序的类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建该类的类,我需要将另一个对象传递给它.
我有一个具有多种形式的应用程序.我有一个基本表单,可以在运行时根据需要从中创建和销毁所有其他表单...目前,我的代码很笨拙,因为我使用了一个"DataObject",其所有内部变量都声明为static,因此我以每种形式拥有的同一对象的所有副本,将使用相同的数据.我非常清楚这不是一个很好的编程方法,但这似乎是我尝试在每种形式中使用相同的"DataObject"问题的临时解决方案.这是我的代码的缩写副本:

I have got a class that when created, I need to have another object passed to it.
I have got an application with multiple forms. I have a base form from which all other forms are created and destroyed as need be during runtime... At the moment, my code is clumsy as I used a ''DataObject'' with all its internal variables declared as static so that with all the copies of the same object I have in each form, the same data would be used. I know very well that this is NOT good programming but this seems to be a temporary solution around my problem of trying to use the same ''DataObject'' in each form. Here is an abbreviated copy of my code:

public class FormA //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public A(ref CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
    //with all other code related to the form...
}

public class FormB //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public A(ref CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
    //with all other code related to the form...
}
//...
//However many forms I will be using...
//...

public class MainForm
{
    CustomDataClass ApplicationData = new CustomDataClass();
    //some code for populating the ApplicationData object with data etc...
    FormA FirstAdditionalForm = new FormA(ApplicationData);
    FormB SecondAdditionalForm = new FormB(ApplicationData);
    //until the number of required forms has been reached...
    
    //on a certain button click method in the MainForm:
    FirstAdditionalForm.ShowDalog();
    //User does what is needed on the form and closes it...
    
    //on a certain button click method in the MainForm:
    SecondAdditionalForm.ShowDalog();
    //User does what is needed on the form and closes it...
}


当我尝试从它们各自的类创建两个表单时,这似乎不起作用.我无法将"ApplicationData"对象作为参数传递到其构造函数中,如果这样做,则存在构建/编译错误等.

我在哪里出错,或者创建新表单类并将数据对象作为引用传递给它的更好方法是什么?这是针对需要使用同一数据对象在表单之间传输数据的多表单应用程序.我希望这可以澄清我的问题.请帮助...


This does not seem to work when I try to create the two forms from their respective classes. I am not able to pass the ''ApplicationData'' object in as a parameter into their constructors and if I do, I have build/compile errors etc.

Where am I going wrong or what is a better way of creating a new form class and passing a data object to it as a ref? This is for a multiple forms application needing data to be transferred between forms using the same data object. I hope this clarifies my problem. Please help...

推荐答案

Winston_D,T.写道:
Winston_D, T. wrote:

似乎不起作用.

您到底是什么意思?
注意:传递对象时通常不需要ref关键字,因为它们已经通过引用传递了(严格地说,对对象的引用通过值传递了),因此使用

What do you mean exactly?
Note: you usually don''t need the ref keyword while passing objects, because they are already passed by reference (strictly speaking a reference to the object is passed by value) hence, using

public class A 
{
    CustomDataClass DataObject;
    public A(CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
}


更改 DatatObject 状态时,实际上是更改原始对象的状态.


when you change DatatObject state you actually change the state of the original object.


如果将Constructor声明为public A(ref CustomDataClass MyCustomCreatedObject),则将类对象传递给ref 关键字.

如果将Constructor声明为public A(CustomDataClass MyCustomCreatedObject),则传递不带ref 关键字的类对象.

在这两种情况下,在构造函数A中修改的对象都将反映类对象MyCustomCreatedObject中的更改.
If you declare Constructor as public A(ref CustomDataClass MyCustomCreatedObject), you pass class object with ref keyword.

If you declare Constructor as public A(CustomDataClass MyCustomCreatedObject), you pass class object without ref keyword.

In both cases, the object modified in constructor A will reflect changes in class object MyCustomCreatedObject.


在Form1上
-------------
public Form2 objForm2;//全局变量
公共字符串testVariable;//全局var

在Form2上
------------------
Form1 objForm1 =新objForm1();
objForm1.objForm2 = this;
objForm1.testVariable =''xyz'';
on Form1
-------------
public Form2 objForm2;//Global var
public string testVariable;//Global var

on Form2
------------------
Form1 objForm1=new objForm1();
objForm1.objForm2=this;
objForm1.testVariable=''xyz'';


这篇关于将ref对象传递给多窗体应用程序的类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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