简体   繁体   中英

Trigger method in other class without new instance/static

I am stuck in this phase of my project. I have 2 window classes.

ClassA
//This window is like a main form which can take you to Class B

ClassB
//This window is a search window that's suppose to set a static object in class A and fire a method `FillData()` in class A.

I can't make this method static since it will require me to change a lot of variables within it's scope to static.

How would you trigger the FillData() method in class A from Class B without using static ? FillData() is suppose to be called when i press an OK button from classB after search.

This is my 2nd week in the C# world :)

If you can use the static keyword, you could simply make the instances of both classes static inside your main class. This way, you don't have to make anything static inside your classes.

public static ClassA Instance1 = new ClassA();
public static ClassB Instance2 = new ClassB();

eg

Program.Instance1.FillData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM