visual-studiovariablesdllprojectsolution

How to use a variable(may be a static) across projects?


I have 3 projects in the same solution. One of the project has the a static variable, which I am trying to access it in the other two projects(by adding the 3rd project's dll). I see that 2 different copies of the static variables are created in the other 2 projects.

How do I solve this problem and access this static variable and make it only one instance across the 2 projects? I mean once it is modified in one project, it should be reflected in the other project.


Solution

  • Of course you can, and even it's very easy.

    Since you've already specify the tag , assuming you are developing with Visual Studio is reasonable.

    I'm further assuming you are writing in c#, but if you are not, there will be a similar approach to do the same thing.

    With c#, in the project you declare the static variable, like

    namespace CrossProject {
        public partial class PartialClass {
            public static String StaticVariable="123";
        }
    }
    

    And you can access StaticVariable in other two projects by specifying

    using CrossProject;
    

    So that you can use it like

    Console.Write("{0}", PartialClass.StaticVariable);
    

    It's not necessarily be a partial class, but notice that variable declaration must be in a class scope.