Style / architecture validation for multiple classes and forms

The C # project that I used to teach code so far has been one gigantic form containing many functions and a huge number of "global" static variables.

Having read here, having done some lessons, etc., now I am trying to go through the refactoring process in order to use classes and encapsulation correctly.

My classes:

  • Program.cs - in principle, does nothing but initialize Form1 .
  • Form1.cs = Creates and modifies the entire user interface. It calls public functions in other classes and has some public functions that other classes can call to update it.
  • UiStrings.cs = Contains all error messages, dialogs, instructions, etc.
  • Options.cs = Monitors some settings, builds and updates paths for installing files, and executes code based on changing the interface.
  • Updater.cs = monitors the current version, downloads assets, puts them in the right place and adjusts the settings.
  • Installer.cs = handles installation / deletion / backup of all assets based on current parameters.

First, I instantiated each of my other classes in Form1 . This worked for Form1 to be able to access functions and properties in UiStrings or Options . However, these classes could not access each other, apparently because they were not created.

What this led me to applies to all of my custom classes, such as UiStrings and Options static (since I don't need multiple instances of any of them). It also means that I have to make every function and property inside each of these classes static too.

Is it a smart decision that someone who really knows what they are doing will come?

It seems that I have worked in my testing so far, but my initial attempt without any classes generally worked very well until I found out how it was wrong.

+4
source share
1 answer

if you open the project properties (right-click on the project in the solution explorer, right-click, select "Properties"), you will find the "Resources" tab for which your UiStrings are intended, and the "Settings" tab for which your UiStrings are intended options. I would recommend using them as a standard feature. It also eliminates the need to develop custom object classes.

In addition, you can make them static, and you do not need to create a copy of the class to access static members. The syntax is ClassName.Method () or ClassName.Property instead of var instance = new ClassName (); instance.Method ();

+2
source

Source: https://habr.com/ru/post/1432619/


All Articles