如何从另一个类访问字段

如何从另一个类访问字段

问题描述:

您好,我有一个带有 2 个窗口的 WPF/C# 应用程序.我正在尝试访问一个

Hello I have a WPF/C# application with 2 windows. I am trying to access a

public int myInt;

在我的其他窗口的主窗口中:

in my MainWindow from my OtherWindow:

MainWindow.myInt =3;

智能感知甚至不允许我访问变量.

intellisense wont even allow me to access the variable.

有人可以帮忙吗?

您需要将其声明为 static 因为您不是通过实例访问它,而是通过类名访问它.也就是说,公开公开字段通常不被认为是好的设计.

You need to declare it as static as you don't access it via an instance, but via class name. That said, it is usually not considered good design to expose fields publicly.