【问题标题】:C#: Communication between main-class and winforms-class. Cannot pass over the dataC#:main-class 和 winforms-class 之间的通信。无法传递数据
【发布时间】:2013-07-17 11:59:32
【问题描述】:

我有一个小问题给我带来了一些问题,我相信这并不难,但对我来说现在是。

我有两个类,一个主类和我的 winform 类。

 foreach (EA.Element theElement in myPackage.Elements)
  {
  foreach (EA.Attribute theAttribute in theElement.Attributes)
    {
     attribute = theAttribute.Name.ToString();
     value = theAttribute.Default.ToString();
     AddAttributeValue(attribute, value);
     }
    }

我在这里获取值并尝试通过这种方法将它们写入数据网格:

private void AddAttributeValue(string attribute, string value)
    {
        int n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells[0].Value = attribute;
        dataGridView1.Rows[n].Cells[1].Value = value;
    }

但是编译器告诉我,AddAttributeValue 不在当前上下文中,我无法调用它。我得到了我想要的值,但不能将它们传递给表单。我知道这听起来微不足道,但我就是不明白。

【问题讨论】:

  • 标准 OOP 问题,您需要对表单对象的引用。并将方法公开。
  • 我一开始就公开了,这不是问题,也不是解决方案,肯定是不同的东西,但还是谢谢你。

标签: c# winforms visual-studio-2010 datagridview


【解决方案1】:

如果我理解的话,提供的代码 sn-ps 属于不同的类。

在这种情况下,方法应该是公开的。

这样:

public void AddAttributeValue(string attribute, string value)
{
    int n = dataGridView1.Rows.Add();
    dataGridView1.Rows[n].Cells[0].Value = attribute;
    dataGridView1.Rows[n].Cells[1].Value = value;
}

【讨论】:

    【解决方案2】:

    公开“AddAttributeValue”:

    public void AddAttributeValue(string attribute, string value)
    

    附录:

    根据我在下面的评论,以下是实现回调的方法,以允许您的主类在您的 winform 中调用方法,否则它没有要引用的实例成员:

    您的 MainClass 将如下所示:

    public static class MainClass
    {
        public delegate void AddAttributeValueDelegate(string attribute, string value);
    
        public static void DoStuff(AddAttributeValueDelegate callback)
        {
            //Your Code here, e.g. ...
    
            string attribute = "", value = "";
    
            //foreach (EA.Element theElement in myPackage.Elements)
            //{
            //    foreach (EA.Attribute theAttribute in theElement.Attributes)
            //    {
            //        attribute = theAttribute.Name.ToString();
            //        value = theAttribute.Default.ToString();
            //        AddAttributeValue(attribute, value);
            //    }
            //}
            //
            // etc...
            callback(attribute, value);
        }
    }
    

    然后在您的 Winform 类中,您可以像这样调用该方法:

    MainClass.DoStuff(this.AddAttributeValue);
    

    这意味着当“DoStuff”完成时,会调用名为“AddAttributeValue”的方法。

    【讨论】:

    • 谢谢,但这不是解决方案,我也公开了。问题是别的。
    • 再看一下您的代码,您似乎正在尝试调用另一个类中的实例成员,但您没有要引用的实例。您的代码正在您的主类中寻找一个名为“AddAttributeValue”的方法。您要么需要获取您的 winform 的实例并引用“AddAttributeValue”的its 实现,要么将 AddAttributeValue 设为静态并通过 MyWinformClass.AddAttributeValue 引用它。 (注意:如果您执行第二个,访问您的 winform 类的特定于实例的成员可能会出现一些问题,例如您的“dataGridView1”成员)。
    • 我建议将回调传递给您的主类。我会在一分钟内提供另一个答案来证明这个想法在工作中。
    • 谢谢瑞秋,在很长一段时间的愚蠢中我引用了错误,这就是为什么我想出了这个微不足道的问题,但谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2016-10-04
    相关资源
    最近更新 更多