user231605
user231605

Reputation: 159

How can I call a method on the current object to another class?

How can I call the Zamkniecie() file Window1.cs not the new object as I showed in the method Window_Closed(), but the object you are currently working with a file MainWindow.cs? Components of methods Zamkniecie() have been created in the file MainWindow.xaml

    File: Window1.cs 
    namespace AllSportsBets102
    {    
        public partial class Window1 : Window
        {
            public Window1(DataGrid zdg, List<Data2> dsx)
            {
                InitializeComponent();
            }
            private void Window_Closed(object sender, EventArgs e)
            {
                MainWindow mw = new MainWindow();
                mw.Zamkniecie();
            }
        }
    }

    File: MainWindow.cs
    namespace AllSportsBets102
    {
        public partial class MainWindow : Window
        {
              public MainWindow()
              {
                  InitializeComponent();
              }
              public void Zamkniecie()
              {
                    InfoStackPanel.IsEnabled = true;
                    KuponStackPanel1.IsEnabled = true;
                    KuponStackPanel2.IsEnabled = true;
                    FiltrStackPanel.IsEnabled = true;
                    WszystkieZdarzeniaStackPanel1.IsEnabled = true;
                    WszystkieZdarzeniaStackPanel2.IsEnabled = true;
                    KuponLabel.IsEnabled = true;
                    WszystkieLabel.IsEnabled = true;
                    InfoLabel.IsEnabled = true;
                    StackPanel1Copy.IsEnabled = true;
                    StackPanel2Copy.IsEnabled = true;  
               }
        }
    }

Upvotes: 0

Views: 279

Answers (2)

Kevin Swarts
Kevin Swarts

Reputation: 435

Here are a few options:

  1. Give Window1 a reference to MainWindow so it can call the Zamkniecie method.
  2. Make the Zamkniecie method static.
  3. Move the Zamkniecie method into Window1.
  4. If there is only one instance of MainWindow, implement MainWindow as a singleton so you will have access to it from anywhere.
  5. If MainWindow instantiates Window1, raise an event from Window1 that is handled on MainWindow.
  6. If MainWindow does not instantiate Window1, implement the observer pattern.

I prefer option 5, if it's possible because it more closely allow each class to manage itself. If you only want to call Zamkniecie when Window1 closes, you should be able to recognize that from MainWindow. Here's a code snippet that would go in MainWindow:

Window1 window1 = new Window1();
window1.ShowDialog();
Zamkniecie();

Note: Which file these classes are in makes no difference as long as they are in the same namespace.

Upvotes: 1

Sam
Sam

Reputation: 143

I know this might be redundant, but the quickest way would be to add an

 using MainWindow.cs;

at the top of the Window1.cs file

Upvotes: 0

Related Questions