Code & QA

Global Variables

The most of applications, especially big and complex ones, need for a kind of variables which must be visible and available all over the application. Sometimes you have to pass data from one module to another, between assemblies, classes, methods, etc. There are different approaches in C# for resolving the problem, and one of them is the use of static variables and methods. In accordance with OOP principles, there are mustn’t be any possibility to create a global variable using object-oriented programming language. And there is no such possibility in C#. Instead, static variables are used.

What is the static member see in MSDN.

In the given framework I use the same variables both in the Mapping and the Tests modules. I’m going to initialize variables in my tests but to use them in the Mapping project passing values of variables there.

For the implementation, I added a new static class to my Extensions.cs file and named it Globals. The class is used for storing those static variables which must be used simultaneously in the Mapping and the Tests modules. Sure, you can keep your static members wherever you wish and it’s not mandatory to have a separate class for your “global” statics. But I think it’s better to have all the mess being collected in one place with user-friendly names.

I added class Globals and put these members into it:

[gist id=b8edac73b2e6cf89c3f4b821f9ba7c96]

Initial values of these variables are kept in App.config file in the Tests project. The method which sets the values will be placed in Extensions class in the Mapping project. It is invoked in BaseTest class in the Tests project. Then the initialized variables are used in the Mapping project and may be used in the tests as well. This is the way how data may be passed from one module to another and between classes and instances inside a module.

App.config file:

[gist id=1e414804d457755a9a2a77425e03dbfb]

The method for static global variables initialization (to avoid errors you have to add reference to System.Configuration library in the Mapping project):

[gist id=82aeba8189a1e59a164d2946c7b37e16]

Usage of the method (don’t forget to rebuild Mapping project before):

[gist id=0b3bb86d26310cbe8ed8559264f5054c]

So the variables initialized in SetGlobals() may be used in any place which has a reference to the Mapping project.
Extension methods                                                            Table Of Content                            WebApplication class

Leave a Reply

Your email address will not be published. Required fields are marked *