How to access fields from another class

后端 未结 2 1563
离开以前
离开以前 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:32

    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.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题