Good day.

The other day I encountered the following problem:

public void SomeMethod() { //здСсь дСйствия со статичСским классом A } public void AnotherMethod() { //Ρ‚Π΅ ΠΆΠ΅ дСйствия, Π½ΠΎ со статичСским классом B } public void SomeAnotherMethod() { //ΠΎΠΏΡΡ‚ΡŒ Ρ‚Π΅ ΠΆΠ΅ дСйствия, Π½ΠΎ со статичСским классом C } 

I repeat the same actions only with different classes, if it were an ordinary class, I would create a generalized method and not be steamed, but the classes are static. Maybe there is a way to somehow get rid of repetitive actions without getting rid of static classes?

Or how to achieve a global scope for instances of non-static classes, if that is easier?

    1 answer 1

    I do not see the problem at all. If I want to work with static classes in this form, I would do this: I would declare a static object field in the App class and switch it at the beginning of each such method. If anything, then you can turn on dynamic for currentObject like this

     class App { public static object currentObject; //public static dynamic object currentObject; public static object classA, classB, classC; } class MyClass { public void SomeMethod() { App.CurrentObject = App.classA; DoWithCurrentClass(); } public void AnotherMethod() { App.CurrentObject = App.classB; DoWithCurrentClass(); } public void SomeAnotherMethod() { App.CurrentObject = App.classC; DoWithCurrentClass(); } void DoWithCurrentClass() { //ДСйствия с CurrentObject } } 
    • Yes, and really a good idea, thank you - Specter