0

How can I send variables by reference in an event handler method?

I've tried using a delegate to send it to another function that holds the variable I needed and it works. However, it creates a copy of the variable and and I can't change the value in the main class from the delegate function. I've then thought of moving the function to a class and store all the variables in the class so they can get and set. However, because it's a class and doesn't inherit the mainclass the WPF/XAML objects can't be found. I've tried inheriting the mainclass but because I'm creating the class inside the mainclass and that it is inheriting itself effectively it gets in a loop it can't escape.

public partial class MainWindow : Window
{

    public class Program : MainWindow
    {
        public string Word { get; set; }

        public void WhenPressed_1(object sender, RoutedEventArgs e)
        {
            lable_1.Content = Word;
        }
    }
    public MainWindow()
    {            
        InitializeComponent();

        Program test = new Program();
        Button_1.Click += delegate (object sender, RoutedEventArgs e) { test.WhenPressed_1(sender, e); };
    }       
}

It's at the line Program test = new Program(); it breaks.

Andrew Foot
  • 119
  • 10
  • I guess you have an unintended Loop/Recursion -> By creating a `new Program` a `new MainWindow`class will be created due `Program` inherits from it and inside the `MainWindow` you create a new `Program` (and the creation/loop starts anew until there is no more "memory space" left -> stackoverflow) – LittleBit Apr 26 '19 at 08:37
  • When you Click a button from Main Window you need to do some process in another class and after that you need to change the label content of main window?? – Justin CI Apr 26 '19 at 09:00
  • I've just used a label (WPF object) to test the code and see what happens. What I'm trying to do is to have an event handler (like a click event) and then do some code. That code requires some variables that are stored in the main window but can't access. – Andrew Foot Apr 26 '19 at 10:46

1 Answers1

1

You can pass MainWindow object to the program class and change the value you want

   public partial class MainWindow : Window, ICommon
{

    public MainWindow()
    {
        InitializeComponent();
        Program test = new Program();
        thisButton.Click += delegate(object sender, RoutedEventArgs e) { test.WhenPressed_1(this); };
    }

    public void SetValueInMain()
    {
        // Set Main values
    }

    public class Program
    {
        public string Word { get; set; }
        public void WhenPressed_1(ICommon mainWind)
        {
            mainWind.SetValueInMain();
        }
    }
}

public interface ICommon
{
    void SetValueInMain();
}
Justin CI
  • 2,693
  • 1
  • 16
  • 34
  • The problem is I don't want to create a new class when the button is clicked. The class is a singleton style class that will hold data that is needed through out the program. I need the data to be accessed as soon as the program starts up (like in the main window). Sorry if the question skips past that as I simplified it down. – Andrew Foot Apr 26 '19 at 10:41
  • @AndrewFoot Then you can declare the Program test = new Program(); inside Mainwindow and access all over the MainWindow? – Justin CI Apr 26 '19 at 10:44
  • @AndrewFoot not much clear ..Edited the code with Interface – Justin CI Apr 26 '19 at 10:53
  • Thank you. I was being an Idiot earlier and didn't see what you did. Thanks for the solution. – Andrew Foot Apr 26 '19 at 10:59