简体   繁体   English

如何从另一个类访问非静态属性

[英]How to access a non-static property from another class

I have a non-static property inside a non-static MainForm class:我在非静态MainForm类中有一个非静态属性:

public string SelectedProfile
{
    get { return (string)cbxProfiles.SelectedItem; }
    set { cbxProfiles.SelectedItem = value; }
}

I would like to get the value of this property, from another non-static class.我想从另一个非静态类获取此属性的值。 Using MainForm.SelectedProfile gives an error saying "An object reference is required for the non-static field, method or property".使用MainForm.SelectedProfile会出现错误,提示“非静态字段、方法或属性需要对象引用”。

Usually I would solve this problem by making SelectedProfile static, but I can't, since cbxProfiles (a ComboBox control) can't be made static.通常我会通过将SelectedProfile设为静态来解决这个问题,但我不能,因为 cbxProfiles(一个 ComboBox 控件)不能设为静态。

So how do I access the property's value without making it static?那么如何在不使其静态化的情况下访问该属性的值呢?

You access non-static members the same way you always do: by using a reference to an instance of the object.您可以像往常一样访问非静态成员:通过使用对对象实例的引用。

So whatever code you want to be able to use that property, you need to pass it a reference to a MainForm object.因此,无论您希望能够使用该属性的代码是什么,都需要向它传递一个对MainForm对象的引用。

As said in the compilation error, you need to have a reference of the existing MainForm instance to act on it.正如编译错误中所说,您需要引用现有的MainForm实例才能对其进行操作。

// You surely do this somewhere in your code
MainForm mainForm = new MainForm();
// ...
// Use the reference to your mainForm to access its public properties
String selectedProfile = mainForm.SelectedProfile;

I might be late to the party but my solution might help someone someday down the road.我可能会迟到,但我的解决方案可能会在未来的某一天帮助某人。 You can directly access an open form's controls (even private ones) using Application.OpenForms[n] ...您可以使用Application.OpenForms[n]直接访问一个开放表单的控件(甚至是私有的)...

For example, let's assume you have created a MainForm and then created a comboBox such that it is inside MainForm => Tab (named tabControl) => TabPage (named tabPageMain) => Panel (named pnlMain) => ComboBox (named cmbSeconds).例如,假设您已经创建了一个 MainForm,然后创建了一个组合框,使其位于 MainForm => Tab(名为 tabControl)=> TabPage(名为 tabPageMain)=> Panel(名为 pnlMain)=> ComboBox(名为 cmbSeconds)。 Then you can access this last control as follows:然后您可以按如下方式访问最后一个控件:

ComboBox combo = Application.OpenForms[0].Controls["tabControl"].Controls["tabPageMain"].Controls["pnlMain"].Controls["cmbSeconds"] as ComboBox;
string SelectedProfile = (string)combo.SelectedItem;

// OR

bool isMaximized = Application.OpenForms[0].WindowState == FormWindowState.Maximized;

ie you've got to traverse the path from the top-level form down to that particular control.即,您必须遍历从顶级表单向下到该特定控件的路径。 Document Outline view of Visual Studio ( View menu => Other Windows => Document Outline ) might help you in this bcoz you might be overlooking some transparent containers in between. Visual Studio 的文档大纲视图(查看菜单 =>其他 Windows =>文档大纲)可能会帮助您解决此问题,因为您可能会忽略中间的一些透明容器。

Use it cautiously.请谨慎使用。 For example, if the handles of any of the referenced controls are not yet created, you might see runtime exceptions.例如,如果尚未创建任何引用控件的句柄,您可能会看到运行时异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据绑定到非静态类上的静态属性? - How to data bind to a static property on a non-static class? 一个非静态类如何调用另一个非静态类的方法? - How can a non-static class call another non-static class's method? 如何从App类访问MainPage类的非静态成员? - How do I access MainPage class non-static members from the App class? 从另一个类调用非静态变量 - Calling a non-static Variable from another Class 从另一个文件/类调用非静态方法 - Calling non-static method from another file/class 从另一个类访问非静态方法而无需实例化 - accessing non-static method from another class without instantiate 如何在Visual Studio中(从另一个非静态方法)在导入的项目中调用非静态方法 - How to call a non-static method in an imported project (from another non-static method) in Visual Studio 非静态类的内部静态类的静态属性是否在非静态类的实例之间共享? - Static property of inner static class of non-static class shared between instances of the non-static class? 另一个类中的非静态变量 - non-static variable in another class 如何从C#中的线程访问非静态方法 - How to access a non-static method from a thread in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM