W
W
wolfak2015-05-31 11:10:06
WPF
wolfak, 2015-05-31 11:10:06

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

2 answer(s)
M
MrDywar Pichugin, 2015-05-31
@wolfak

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.

A
Artyom, 2015-05-31
@artem_b89

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;}
    }

Now the Text property can be accessed like this:
Here is another example application: https://dl.dropboxusercontent.com/u/18441732/test/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question