Global Variable not retaining value between WPF pages

Viewed 25

I have multiple WPF windows, and store the variables in a C# class for accessibility between windows. But when setting a value to an int, instead of setting it to that value it sets it to 0.

public class CommonClass
   {
       public static int SemesterWeeks;
       
   }
}

In this WPF window, I set the value of SemesterWeeks to 20 for example

public partial class NumWeeksWindow : Window
    {
        public NumWeeksWindow()
        {
            InitializeComponent();
            CommonClass.SemesterWeeks = Parsing(SemesterWeeksText.Text);
        }

        private static int Parsing(string s)
        {
            CommonClass.check = false;
            if (!string.IsNullOrEmpty(s))
            {
                if (int.TryParse(s, out int value))
                {
                    CommonClass.check = true;
                    return value;
                }
                MessageBox.Show($"{s} in an invalid value, please input a number.");
            }
            return 0;
        } 

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            ListSelfStudyWindow LSSW = new ListSelfStudyWindow();
            this.Close();
            LSSW.Show();
        }     
    }
 public partial class ListSelfStudyWindow : Window
    {       

        public ListSelfStudyWindow()
        {
            InitializeComponent();
                
                Label1.Content = CommonClass.SemesterWeeks;
            }            
        }

When setting the content of label1 to see what value SemesterWeeks has, it shows 0.

Is there something I am doing wrong or to fix it so that the variable retains it's value.

0 Answers
Related