【发布时间】:2014-12-22 11:29:30
【问题描述】:
我对@987654321@ 很陌生,我无法从另一个文件中找到驻留在一个文件中的变量、表单属性和其他一些方法。我觉得,这应该像 #include 指令一样简单,但找不到方法。
这是我在 VS 中创建新的 Windows 窗体应用程序时自动创建的文件 Form1.cs 中的代码。我可以访问位于另一个名为 Class1.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 reach_test {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Class1 formName = new Class1(); // this line compiles OK.
formName.someMethod(); // this line compiles OK.
this.Text = "Some Header"; // this line compiles OK.
}
}
}
这是Class1.cs 文件中的代码,这是我在之后添加的。例如,我无法访问位于文件Form1.cs 中的Form1.Text 属性:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace reach_test {
public class Class1 {
public string myText;
public Class1() {
myText = "Windows Header";
}
public void someMethod() {
Form1.Text = myText; // this line does not compile!
}
}
}
错误信息是:An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Text.get'
【问题讨论】: