【问题标题】:An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.label1'非静态字段、方法或属性“WindowsFormsApplication1.Form1.label1”需要对象引用
【发布时间】:2012-11-21 23:46:26
【问题描述】:

我想重用一段代码,所以我想我会用一个包含该代码的方法创建一个类,然后我会在需要的地方调用该方法。

我做了一个简单的例子来说明我的问题是什么:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            LoadText.getText();
        }
    }
}

LoadText.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    public class LoadText : Form1
    {
        public static void getText()
        {
            WindowsFormsApplication1.Form1.label1.Text = "New label1 text";
        }
    }
}

如您所见,我有一个带有标签的表单,我想使用我的其他方法(LoadText 中的 getText)来更改标签的文本。

这是我的错误信息:

非静态字段、方法或属性“WindowsFormsApplication1.Form1.label1”需要对象引用**

我已经在设计中将 label1 从私有更改为公开。

我该如何解决这个问题?

【问题讨论】:

    标签: c# methods


    【解决方案1】:

    问题在于Form1 是一个类,而不是一个对象。 label1 不是类的静态成员,它是Form1instance 的成员。因此出现错误,它告诉您需要一个对象实例(Form1 类)。

    尝试以下方法:

    Form1.cs:

    LoadText.getText(label1);
    

    LoadText.cs:

    public static void getText(Label lbl)
    {
        lbl.Text = "New label1 text";
    }
    

    您现在有了一个静态方法,该方法将接受 Label 对象并将其文本设置为“新 label1 文本”。

    有关static 修饰符的更多信息,请参阅以下链接:

    http://msdn.microsoft.com/en-us/library/98f28cdx.aspx

    HTH

    【讨论】:

      【解决方案2】:

      这是 OO 编程新手的常见问题。

      如果你想使用一个对象的方法,你需要创建它的一个实例(使用new)。除非,该方法不需要对象本身,在这种情况下,它可以(并且应该)被声明为静态的。

      【讨论】:

        【解决方案3】:

        我尝试了另一种也有效的方法:

        Form1.cs:

         // here a static method is created to assign text to the public Label
         public static void textReplaceWith(String s, Label label)
         {
             label.Text = s;
         }
        

        LoadText.cs:

        namespace WindowsFormsApplication1
        {
            public class LoadText : Form1
            {
                //new label declared as a static var
                public static Label pLabel;
        
                //this method runs when your form opens
                public LoadTextForm() 
                {
                    pLabel = Label1; //assign your private label to the static one
                }
        
                //Any time getText() is used, the label text updates no matter where it's used
                public static void getText()
                {
                   Form1.textReplaceWith("New label1 text", pLabel); //Form1 method's used 
                }
            }
        }
        

        这将允许您使用公共方法从几乎任何地方更改标签的文本变量。希望这会有所帮助:)

        【讨论】:

          【解决方案4】:

          您需要对表单的引用才能访问其元素。

          【讨论】:

            猜你喜欢
            • 2013-11-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多