I have a public Library class with all the global variables needed for my program to work. It is structured like this:
public static class Library
{
public static string globalString = "";
}
In other languages, it is possible to use extends
to extend a class to another class so the 'Library' prefix is not required on global variables but in C# it works a bit differently. I tried adding it after the colon in the class I wanted to extend to (like this: public partial class Login : Form, Library
) and I was told that "Classes cannot have multiple base classes". Apparently, it is possible to use interfaces to extend classes but I do not fully understand how that works or how to implement it.
If you're using one of the newer versions of C#, you can import a static class so that you use its members without fully qualifying their names. In C#, the syntax would be...
using static RootNamespace.Library;
// Provided the class Library is in a namespace called RootNamespace
The above line would be placed at the top of your code file, in this case I'm assuming it's in Login.cs. This will be among the other using statements like using System;
and you will be able to use the public static members of Library
inside your Login class.
I would recommend using something other than global variables though, such as properties so that you can encapsulate some logic and protect against invalid states by running some validation before these values are changed.