Hello I have a WPF/C# application with 2 windows. I am trying to access a
您好我有一个带有2个窗口的WPF / C#应用程序。我正在尝试访问
public int myInt;
in my MainWindow from my OtherWindow:
从我的OtherWindow在我的MainWindow中:
MainWindow.myInt =3;
intellisense wont even allow me to access the variable.
intellisense甚至不允许我访问变量。
Could someone please help?
有人可以帮忙吗?
2 个解决方案
#1
4
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.
您需要将其声明为静态,因为您不通过实例访问它,而是通过类名称。也就是说,公开曝光字段通常不被认为是好的设计。
#2
3
You either need to have an object:
你需要有一个对象:
MainWindow mw = new MainWindow();
mw.myInt = 3
Or you need to make rour field static
或者你需要使rour field静止
public static int myInt;
and call it like your are already doing:
并称之为你已经在做的事情:
MainWindow.myInt =3;
#1
4
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.
您需要将其声明为静态,因为您不通过实例访问它,而是通过类名称。也就是说,公开曝光字段通常不被认为是好的设计。
#2
3
You either need to have an object:
你需要有一个对象:
MainWindow mw = new MainWindow();
mw.myInt = 3
Or you need to make rour field static
或者你需要使rour field静止
public static int myInt;
and call it like your are already doing:
并称之为你已经在做的事情:
MainWindow.myInt =3;