How to access fields from another class

后端 未结 2 1562
离开以前
离开以前 2021-01-17 07:13

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:



        
2条回答
  •  抹茶落季
    2021-01-17 07:40

    You either need to have an object:

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

    Or you need to make rour field static

    public static int myInt;
    

    and call it like your are already doing:

    MainWindow.myInt =3;
    

提交回复
热议问题