Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global object in a C# library [duplicate]

Possible Duplicate:
Best way to make data (that may change during run-time) accessible to the whole application?

I have a C# library.

  1. Can a library have global objects/variables?
  2. Can an initialization method for those objects be automatically executed from a library when running the main project or do I have to make it a static method and run it from the main project?
like image 571
Stefanos Kargas Avatar asked Jan 13 '13 12:01

Stefanos Kargas


People also ask

Can we create global object?

Yes, you can declare a global variable of any type, class or not. Or you can have a "setter" function that is used to set or reinitialize the object.

How do you create a global object in C++?

To declare global variables in C++, we can declare variables after starting the program. Not inside any function or block. If we want to declare some variables that will be stored in some different file, then we can create one file, and store some variable.

What is global object in C#?

A global object is an object that always exists in the global scope.

How do you declare a global variable in C sharp?

In C# you cannot define true global variables (in the sense that they don't belong to any class). You can then retrieve the defined values anywhere in your code (provided it's part of the same namespace ): String code = Globals. CODE_PREFIX + value.

What is a global object in C++?

In the C++ language generally we create an object of class in a main () function or member function but it is also possible to create a object globally. Such object are called global object.

How do I create a global variable in a form?

However, you can make a global variable by creating a static class in a separate class file in your application. First, create a class called global in your application with the code given below. The class also has a static variable. Now, you can access it from anywhere in your forms after assigning a value to it.

What is global scope in C with example?

Global scope. By default, global variables are of global scope. Which means we can access a global variable everywhere in same as well as other C programs (using extern). Example program to use global scope variables. First let us create a C program that contains only global variables, save the below program with name global.c.

What are global variables in C++?

Global variables are variables declared outside a function. Unlike local variables and static variables, a global variable is not declared inside a function. Properties of a global variable Global variables are allocated within data segment of program instead of C stack.


2 Answers

In C# I always use a static classes to provide this functionality. Static classes are covered in detail here, but briefly they contain only static members and are not instantiated -- essentially they are global functions and variables accessed via their class name (and namespace.)

Here is a simple example:

public static class Globals
{
    public static string Name { get; set; }
    public static int aNumber {get; set; }
    public static List<string> onlineMembers = new List<string>();

     static Globals()
     {
        Name = "starting name";
        aNumber = 5;
     }
}

Note, I'm also using a static initializer which is guaranteed to run at some point before any members or functions are used / called.

Elsewhere in your program you can simply say:

Console.WriteLine(Globals.Name);
Globals.onlineMemeber.Add("Hogan");

Static objects are only "created" once. Thus everywhere your application uses the object will be from the same location. They are by definition global. To use this object in multiple places simply reference the object name and the element you want to access.


You can add static members to any class and they will be globally available, but I think having one place for globals is a better design.

like image 189
Hogan Avatar answered Oct 09 '22 04:10

Hogan


You can use public static properties on a class as global objects/variables.

You can initialize static properties in a static constructor for the class, which will be called directly before the first time the properties are accessed.

like image 32
Ergwun Avatar answered Oct 09 '22 06:10

Ergwun