1

I want to define a global structure in C# and use it in three separate subroutines. I have already created the structure but I don't know how to make it a global variable so I can use it in multiple sections of my code. Any help is appreciated.

    public struct Simple
    {
        public int Position;
        public bool Exists;
        public double LastValue;
    };


    static void Main(string[] args)
    {
        Simple s;
        s.Position = 1;
        s.Exists = false;
        s.LastValue = 5.5;
    }

So I want to use a Simple structure in two other routines in my code and possible pass it to different form (multiple usage of one variable).

5
  • 1
    What do you mean by structure? Can you show us one of the implementations of the "local" structure?
    – James
    Aug 10, 2012 at 15:26
  • 2
    It's worth noting that global variables should be avoided whenever possible. There are cases where you may use them, but as a rule you should always scope your variables in the smallest feasible scope.
    – Servy
    Aug 10, 2012 at 15:27
  • by structure I mean "struct" I will add a simple code to my question
    – Amir
    Aug 10, 2012 at 15:28
  • 1
    Please don't. All kinds of bad things can result. It's really poor practice. Aug 10, 2012 at 15:28
  • 1
    You're probably looking for a Singleton. Are you sure you want to do that? I've never had need for a "Global Variable Structure" in my programs; perhaps Settings fits the bill? Aug 10, 2012 at 15:28

2 Answers 2

2

The closest thing to "global" in C# is "static". Simply define the class and all members as static and it'll be accessible from anywhere the containing namespace is referenced. EDIT as Servy correctly points out, the class itself does not have to be static; however doing so forces all members to be static at compile-time. Also, static members can have any visibility, so you can have a private static field used by a public static property or method, and you can have an internal static class that won't be visible outside its home assembly. Just being static doesn't automatically make it wide open.

However, a better pattern might be the Singleton; you define a class that has one static instance of itself, that can then be passed around. The benefit is that you can still deal with the object as an instance class if you want to, but the same instance is available everywhere using a static getter. Here's some reading material: http://csharpindepth.com/Articles/General/Singleton.aspx

3
  • 1
    It also needs to be public. You can have private static variables. Additionally there is no need for the class to be static.
    – Servy
    Aug 10, 2012 at 15:28
  • Having a singleton is still a global variable. It's just that the global variable is the equivalent of a pointer to something, rather than actually being that something.
    – Servy
    Aug 10, 2012 at 15:31
  • Ah, good point, thanks for the hint. Now I have one more question, Can I pass a class to different form and edit it there???
    – Amir
    Aug 10, 2012 at 15:31
1

In your case it appears that you have a object as a local variable in your main method that you need to use in another method. The appropriate solution in this context is to add a parameter to that other method. Take a look at this example:

public class MyObject
{
    public int Value;
}

public static void Main(string[] args)
{
    MyObject obj = new MyObject();

    obj.Value = 42;

    PrintObject(obj);

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(true);
}

public static void PrintObject(MyObject obj)
{
    Console.WriteLine(obj.Value);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.