C# 非静态字段、方法或属性需要对象引用';WindowsFormsApplication1.Form1.label1';
我想重用一段代码,所以我想我会用一个包含该代码的方法创建一个类,然后在需要的地方调用该方法 我举了一个简单的例子来说明我的问题: Form1.csC# 非静态字段、方法或属性需要对象引用';WindowsFormsApplication1.Form1.label1';,c#,methods,C#,Methods,我想重用一段代码,所以我想我会用一个包含该代码的方法创建一个类,然后在需要的地方调用该方法 我举了一个简单的例子来说明我的问题: 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.Threadi
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();
}
}
}
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.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();
}
}
}
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从私人更改为公共
如何解决此问题?您需要对表单的引用才能访问其元素。问题在于
Form1
是一个类,而不是一个对象label1
不是类的静态成员,它是Form1
实例的成员。因此出现了错误,它告诉您需要一个对象实例(属于Form1
类)
请尝试以下操作:
Form1.cs:
LoadText.getText(label1);
public static void getText(Label lbl)
{
lbl.Text = "New label1 text";
}
// here a static method is created to assign text to the public Label
public static void textReplaceWith(String s, Label label)
{
label.Text = s;
}
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
}
}
}
LoadText.cs:
LoadText.getText(label1);
public static void getText(Label lbl)
{
lbl.Text = "New label1 text";
}
// here a static method is created to assign text to the public Label
public static void textReplaceWith(String s, Label label)
{
label.Text = s;
}
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
}
}
}
现在有了一个静态方法,它将接受一个标签对象,并将其文本设置为“newlabel1 text”
有关静态
修改器的更多信息,请参见以下链接:
这是OO编程新手的一个常见问题
如果要使用对象的方法,则需要创建对象的实例(使用new)。除非,否则该方法不需要对象本身,在这种情况下,可以(而且应该)将其声明为静态。我尝试了另一种方法,该方法同样有效:
Form1.cs:
LoadText.getText(label1);
public static void getText(Label lbl)
{
lbl.Text = "New label1 text";
}
// here a static method is created to assign text to the public Label
public static void textReplaceWith(String s, Label label)
{
label.Text = s;
}
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
}
}
}
LoadText.cs:
LoadText.getText(label1);
public static void getText(Label lbl)
{
lbl.Text = "New label1 text";
}
// here a static method is created to assign text to the public Label
public static void textReplaceWith(String s, Label label)
{
label.Text = s;
}
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
}
}
}
这将允许您使用公共方法从任意位置更改标签的文本变量。希望这有帮助:)