【发布时间】:2020-07-27 15:26:52
【问题描述】:
我在非静态 MainForm 类中有一个非静态属性:
public string SelectedProfile
{
get { return (string)cbxProfiles.SelectedItem; }
set { cbxProfiles.SelectedItem = value; }
}
我想从另一个非静态类中获取此属性的值。使用 MainForm.SelectedProfile 时会出现错误提示“非静态字段、方法或属性需要对象引用”。
通常我会通过将SelectedProfile 设为静态来解决此问题,但我不能,因为 cbxProfiles(ComboBox 控件)无法设为静态。
那么如何在不使其成为静态的情况下访问该属性的值呢?
【问题讨论】:
-
您需要一个
MainForm类的实例。 -
I would like to get the value of this property, from another non-static class.在创建该类时,将 MainForm 的引用传递给它。 (new OtherClass(this))