How to call a non-static function from a static one? Or get access from a static function to tw_tree?

A non-static field, method, or property "Window1.ReloadT ()" requires an object reference.

Window2

public void TextReload() { Window1.ReloadT(); 

Window1

 public void ReloadT() { tw_tree.ItemsSource = null; } 

When changing public void ReloadT() to public static void ReloadT() , tw_tree can no longer be used:

A non-static field, method, or property "Window1.tw_tree" requires an object reference.

This is how a static function call normally occurs:

Window2

 Window1.SplitArguments(); 

Window1

 public static string[] SplitArguments(){} 
  • 3
    No way: a static function does not need an object, and no static one is needed. If there is no object, no non-static can be called. The maximum can be transferred to the static function of the object that is being worked on. But since an object is used in a static function, it's easier to make it non-static - Grundy
  • <TreeView x:Name="tw_tree" - The TreeView belongs to the Window1 window. How then to access it from the second Window2 window? - codename0082016
  • get in Window2 - a link to the window1 object and get the tw_tree property from it or pass this property immediately - Grundy
  • But this is more like a completely different question than the original one. Perhaps it is worth asking it separately with examples of xaml, and the code where and how you are going to receive what - Grundy
  • one
    @ codename0082016 I strongly advise you to read about the architectural pattern MVVM, which is used when creating WPF applications. - Artem Nikolayevich

2 answers 2

You can, for example, pass an instance of Window1 to Window2.

 class Window2 : Window { Window1 window1; public Window2(Window1 window1) { this.window1 = window1; } public void TextReload() { window1.ReloadT(); } } 

Or create an event to subscribe to in Window1.

 class Window2 : Window { public event EventHandler OnTextReload; public void TextReload() { OnTextReload?.Invoke(this, EventArgs.Empty); } } class Window1 : Window { private void HandleTextReload(object sender, EventArgs e) { ReloadT(); } public void ShowWindow2() { var w2 = new Window2(); w2.OnTextReload += HandleTextReload; w2.ShowDialog(); ... } } 
  • window1.ReloadT(); - Дополнительные сведения: Ссылка на объект не указывает на экземпляр объекта. - codename0082016
  • one
    This means that you pass null Window2 constructor. Creating Window2 inside Window2 methods will look something like this: var w2 = new Window2(this) - Vlad

Class members (fields, properties, methods) can be static or instance. Static can be accessed only in the context of a class, to instance - in the context of an object (you need to create an object of a class and access through it).

 public class Car { public static string Model; // Статическое поле public string Color; // Экземплярное поле } Car.Model = "ElectroCar"; // Обращаемся через класс Car var myCar = new Car(); myCar.Color = "Black"; // Обращаемся через экземпляр (объект) 

It is impossible to use non-static members inside static members without initializing the object. To call an instance method, you must either create an object of this class, or transfer it from outside.

Making members static is necessary if they are common to all objects. For example, there are football players and a ball. We could have made the ball non-static, but then everyone would play with his own ball — a team game would not work. The ball must be general (static).

In your case, tw_tree refers to the object. If you make the ReloadT () method static, you will not be able to use tw_tree when accessing it. the object to which tw_tree belongs to was not created.


Learn more about static https://professorweb.ru/my/csharp/charp_theory/level5/5_12.php

  • <TreeView x:Name="tw_tree" - The TreeView belongs to the Window1 window. How then to access it from the second Window2 window? - codename0082016