Question

How can I access a variable in MainWindow.xaml.cs that is defined as public from Window1.xaml.cs?

I thought that if a variable is defined that way I will be able to access it from anywhere. What am I missing here?

Était-ce utile?

La solution

I thought that if a variable is defined that way I will be able to access it from anywhere

Unless the variable is both public and static, you can't access it from almost anywhere coz that should be accessed based on a certain instance of an object.

If you wanna access a variable from any window, either make it static or call it based on an instance of that window like

Window1 win = new Window1();
win.Variablex = 1;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top