Answer the question
In order to leave comments, you need to log in
How to change public variable from another page (WPF)?
Hello.
There is one window (MainWindow) and several pages which are displayed in one window.
In the main window declared a variable
public int savesass = 0;
In another window, I read this variable like this (Output in textbox):
MainWindow mmm = new MainWindow();
texttext.Text = mmm.savesass.ToString();
But how to change the value of this variable?
This way does not work:
MainWindow mmm = new MainWindow();
mmm.savesass = 5;
Maybe I'm doing something wrong? Then how to make one variable for all pages, which can also be changed?
Thank you very much for your help!
Answer the question
In order to leave comments, you need to log in
The new operator , read it.
You create a new instance, and only change or read the value in it. The static
modifier .
Allows you to make an object in a single instance and shared.
The new operator creates a new instance of a class. I advise you to take a book on programming in C # and read, or even better, two:
First www.ozon.ru/context/detail/id/3658608
Then www.ozon.ru/context/detail/id/21236101
Regarding your question, I already answered : Use static class or singleton.
Here is an example of a singleton:
public class CurrentContext
{
private CurrentContext()
{
}
private static Lazy<CurrentContext> instance = new Lazy<CurrentContext>(() => new CurrentContext());
public static CurrentContext Instance
{
get { return instance.Value; }
}
public string Text {get;set;}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question