-2

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

public int myInt;

在我的 OtherWindow 的 MainWindow 中:

MainWindow.myInt =3;

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

有人可以帮忙吗?

4

2 回答 2

4

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

于 2012-09-19T21:39:13.367 回答
4

您要么需要一个对象:

MainWindow mw = new MainWindow();
mw.myInt = 3 

或者您需要将 rour 字段设为静态

public static int myInt;

并像您已经在做的那样称呼它:

MainWindow.myInt =3;
于 2012-09-19T21:40:25.823 回答