The c# project I’ve been using to teach myself to code has so far been one gigantic form containing tons of functions and a huge amount of “global” static variables.
After reading here, doing some tutorials etc, I’m now trying to go through the process of refactoring it to properly use classes and encapsulation.
My classes:
Program.cs– Basically does nothing but initializeForm1.Form1.cs= Generates and modifies all the UI. Calls public functions in the other classes, and has some public functions other classes can call to update it.UiStrings.cs= Contains all the error messages, dialogs, instructions etc.Options.cs= Tracks some settings, builds and updates paths to install files, executes code based on changing UI.Updater.cs= Tracks current version, downloads assets, puts them in the right place, and adjusts the options.Installer.cs= Handles installing/uninstalling/backing up all the assets, based on the current options.
Initially, I created an instance of each of my other classes in Form1. This worked for Form1 to be able to access functions and properties in UiStrings or Options. However those classes couldn’t access parts of each other, presumably because they hadn’t been instantiated.
What this has led me toward is making all my custom classes like UiStrings and Options static (since I don’t need multiple instances of any of them). This also means I have to make every function and property inside each of those classes static too.
Is this a reasonable solution that someone who actually knows what they’re doing would come to?
It seems to work in my testing so far, but my original attempt with no classes at all worked just fine too until I learned how wrong it was.
if you open project properties (right click on project in Solution explorer, right click, select Properties), you will find Resources tab that is what your UiStrings is for, and Settings tab which is what your Options is for. I would recommend using them as a standard feature. This also eliminates need to design your own feature classes.
Other than that you could do them static, and you dont have to create a copy of a class to access static members. Syntax is ClassName.Method() or ClassName.Property instead of var instance = new ClassName(); instance.Method();