在表单之间传递数据为何如此复杂?!〜已解决 [英] Passing data between forms why so complicated?!~SOLVED

查看:65
本文介绍了在表单之间传递数据为何如此复杂?!〜已解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

更新:解决方案1已接受!

我已经用C#编程了几周(几乎是全职),每天都在学习.我目前正在尝试整理项目中的数据.此问题是我上一个问题在表单之间传递数据的后续问题.例如,我有一些要调整的设置.这些设置以主要形式使用,但以其他形式设置.

我想在我的项目中有一个中心点,可以在其中存储所有设置(变量).重要的是,我可以访问和更新项目中所有来源的数据.

我创建了一个带有所有默认值声明的类.像这样:

Hey,

UPDATE: Solution 1 accepted!

I''ve been programming in C# for a few weeks (nearly fulltime) and learning every day. I am currently trying to organize the data in my project. This question is a followup question from my previous question Passing data between forms. I have for example some settings which I want to adjust. These settings are used in the main form but set in a different form.

I''d like to have a central point in my project where I can store all my settings (variables). It is important that I can acces and update the data from all froms in my project.

I create for example a class with all the declarations with a default value. like this:

namespace SerialProgrammer
{
    class Settings
    {
        public decimal Density = 21;
        public string SelectedPrinterName = "ZDesigner GX430t";
    }
}




所以我想在form2中调整此值;像这样:




So I want to adjust this value in form2; like this:

Settings settings = new Settings();
private void BTN_Set_Click(object sender, EventArgs e)
{

    settings.Density = nudDensity.Value;
    this.Close();

}



在我的主表单上,我想使用设置,如下所示:



And on my main form I want to use the settings, like this:

Settings settings = new Settings();
private void printLabel(string snum, string type, int number)
{
    if (IsPrinterOnline(settings.SelectedPrinterName))
    {
        if (settings.SelectedPrinterName.Contains(Dymo_PrinterName))
        {
            dymoPrintLabel(snum, type, number);
        }
        else if (settings.SelectedPrinterName.Contains(Zebra_PrinterName))
        {
            ZebraPrintLabel(snum, type, number);
        }

    }
}



我所经历的是,即使看起来(至少对我而言)这两种形式都共享类设置,但它们都使用具有不同值的不同类设置.如何更改我的代码,使我拥有一组可以从两种形式轻松访问的设置?



What I experienced is that even though it looks like (at least for me) that both forms share the class Settings, they both use a different class Settings with different values. How can I change my code that I have one set of settings which are easily accessable from both forms?

推荐答案

您的另一种可能性是通过使用单例模式具有静态对象.
请为您的班级查看以下代码:
Another possibility for you would be to use the singleton pattern by having a static object.
Please see this code for your class:
class Settings
{
   private Settings()
   {
   }
   static Settings m_instance = null;
   public static Settings Core
   {
      get
      {
            if (m_instance == null)
               m_instance = new Settings();
            return m_instance;
      }
   }
   public decimal Density = 21;
   public string SelectedPrinterName = "ZDesigner GX430t";
}


然后,您可以使用以下代码从任何地方使用该类:


You can then use the class from wherever you want using the follwing code:

public Form1()
{
   InitializeComponent();
   Settings.Core.Density = 5;
}


您正在共享相同的类(类型),但不共享该类的相同实例(对象).
如果您想拥有这样的全局"对象,甚至最好在单例模式中使用llok,则应该探索关键字static的用法.您将在代码项目中找到很多有关它的文章.
You are sharing the same class (Type) but not the same instance of that class (Object).
You should explore the usage of the keyword static if you want to have such "global" object or even better have a llok at the singleton pattern. You''ll find plenty of articles in code project about it.


这是有关表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [ http://en.wikipedia.org/wiki/Accidental_complexity [ http://en.wikipedia.org/wiki/Loose_coupling [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于在表单之间传递数据为何如此复杂?!〜已解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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