在每个表单中使用一个数据对象的多表单应用程序 [英] Multiple forms application using one data object across each form

查看:85
本文介绍了在每个表单中使用一个数据对象的多表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建该类的类,我需要将另一个对象传递给它.
我有一个具有多种形式的应用程序.我有一个基本表单,可以在运行时根据需要从中创建和销毁所有其他表单...目前,我的代码很笨拙,因为我使用了一个"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 suggested code that I am trying to use:

public class FormA //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public FormA(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 FormB(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"对象作为参数传递到其构造函数中,如果这样做,则存在构建/编译错误等.

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


My Problem:
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...

推荐答案

以下是错误:

声明public A不是有效的方法,因为它没有声明返回类型.它可能是有效的构造函数(因为它始终返回声明的类或结构的实例,所以从不声明返回类型),但是在这种情况下,应为它指定与类相同的名称.如果将public A更改为public FormApublic FormB,它将起作用.

您需要从类类型的参数中删除"ref".正式地,您可以将按引用传递用作引用类型(在您的情况下为类)的参数,但这很愚蠢.这意味着逐个引用地传递引用,双重引用(间接),这没有任何意义.

当您通过引用传递引用类型的对象(如类)时,它将完全满足您的目的.在您的情况下:您将必须形成实例,其中包含对CustomDataClass的同一对象的不同引用.如果通过一种形式修改数据,则将以另一种形式修改数据,因为尽管两个不同的引用都引用了该对象,但它是同一对象.我想这就是你想要的.

如果您完成了这两个修复程序,则看起来不错.

—SA
Here are the bugs:

The declaration public A is not a valid method as it does not declare a return type. It could be a valid constructor (which never declared return type because it always returns the instance of the declaring class or structure), but in this case you should give it the same name as the class. If you change public A to public FormA and public FormB, it will work.

You need to remove "ref" from the parameter of the class types. Formally, you can use passing by reference for the parameters of reference types (the class in your case), but this is simply silly. This means passing a reference by reference, double referencing (indirection), which makes no sense.

When you pass an object of a reference type (like a class) by reference, it will perfectly serve you purpose. In your case: you will have to form instances holding different references to the same object of CustomDataClass. If you modify data through one form, it will be modified in another form, as this is the same object albeit referenced by two different references. I think this is what you wanted.

It looks like if you make these two fixes you are good to go.

—SA


您正在尝试在对象初始化程序中构造FirstAdditionalForm和SecondAdditionalForm.将新语句移至MainForm的构造函数中
You''re trying to construct the FirstAdditionalForm and SecondAdditionalForm in the object initializer. Move the new statements to your constructor for MainForm


这篇关于在每个表单中使用一个数据对象的多表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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