Pk King X11
Pk King X11

Reputation: 163

How can I extend a form class to access global variables without having to specify which class it belongs to?

I'm trying to create some global variables that I can access across all classes in my program and use them as if they exist in the same context of each class, i.e. without typing 'gVariables.'

class gVariables
{
    public static string VariableA;
    public static int VariableB;
    public static byte[] VaraibleC;
    public static bool AppIsClosing = false;
}

This works in a standard class:

public class SomeClass : gVariables
{
    private void SomeMethod()
    {
        //Dont need to type 'gVariables.'
        VariableA = "FooBar";
        VariableB = 1024;
    }
}

But how can I do this in a class that inherits System.Windows.Forms.Form?

Upvotes: 0

Views: 1600

Answers (2)

sampathsris
sampathsris

Reputation: 22280

One way would be to make a base class that has all the globals as members, as Donal has shown you. But as you have said in the comments, you cannot have a common base class for all your classes.

I will tell you a way you can do this, but I am not recommending that you should do this. What I am going to tell you is a way to simulate multiple inheritance in C# (again, not recommended if you do not have a good reason), so you can inherit both your global variables class and the natural parent class.

You can use an interface, and make all your classes implement that interface. To make examples shorter, I will choose only one variable: AppIsClosing.

interface IGlobalVariables
{
    public bool AppIsClosing { get; set; }
}

You should have your class containing global variables, which implements IGlobalVariables. To make sure that only one instance is there, we will also implement the singleton pattern.

class GlobalVariables : IGlobalVariables
{
    private bool _appIsClosing = false;
    private static GlobalVariables _instance = new GlobalVariables();

    private GlobalVariables() {}

    public static Instance {
        get { return _instance }
    }

    public bool IGlobalVariables.AppIsClosing
    {
        get { return this._appIsClosing; }
        set { this._appIsClosing = value; }
    }
}

Then in all other classes, you can do this:

class SomeClass : WhateverYouWantTheParentClassToBe, IGlobalVariables
{
    public bool IGlobalVariables.AppIsClosing
    {
        get { return GlobalVariables.AppIsClosing; }
        set { GlobalVariables.AppIsClosing = value; }
    }

    // other code
}

Assuming you heavily use global variables, now you can refer to the global variable as just AppIsClosing anywhere in your SomeClass.


Your requirement to use global variables (I believe) is not some evil use of global variables. Global variables are bad when you make int temp a global variable (or any other variable that should be local).

The problem I see with my approach is, you can never tell when a member of GlobalVariables is accessed. For example if we try to find references of GlobalVariables.AppIsClosing, Visual Studio will show us a bunch of IGlobalVariables.AppIsClosing implementations, which is utterly useless.

Just use your global class. Having to type few characters is one of the worst reasons to make this kind of awkward design.

I am showing this pattern to you because we had to port a software framework that was written in a legacy language which had multiple inheritance. And this is what we did. We simulated multiple inheritance by interfaces and object aggregation. I think we had a good reason there to use this kind of awkward design patterns.

Upvotes: 0

Donal
Donal

Reputation: 32713

You can create a BaseForm that inherits from Form. And then in each of your Forms (e.g. DerivedForm) you can inherit from the BaseForm. For example:

public class BaseForm : Form
{
    public static string VariableA;
    public static int VariableB;
    public static byte[] VaraibleC;
    public static bool AppIsClosing = false;

}

public class DerivedForm : BaseForm
{

}

Upvotes: 1

Related Questions