#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question C# How to declare, and use, solution/project level variables?


    Hi all,

    I'd like to create some variables that are available throught a C# solution, so I don't have to declare them over and over within each method that will use them.

    For example. There are two variables that are used very often, in many methods: errorTxt and infoTxt (for logging purposes).

    I have a solution, with 2 projects in it. Lets say, project A and B.

    Within the A project, I've created a class file called GlobalVars.cs. The B project, b.t.w. has a project dependence on project A (which contains the GlobalVars.cs file).

    GlobalVars.cs (with just one string declaration for example):
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace A
    {
        public static class GlobalVars
        {
            static string s_infoTxt = "";
    
            public static string infoTxt
            {
                get { return s_infoTxt; }
                set { s_infoTxt= value; }
            }
    
        }
    }
    How can I use the infoTxt string variable outside this GlobalVars.cs file, and assign it different strings withing different Methods found in both project A and B?

    [Edit: Add] b.t.w. I've tried accessing that variable in a class file in project B, like this:

    Code:
    GlobalVars.infoTxt = "some text";
    Error: The name 'GlobalVars' does not exist in the current context

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Have you tried adding a reference to Project A in Project B? If that doesn't work, another option might be to use .Net Reflection.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Originally Posted by BobS0327
    Have you tried adding a reference to Project A in Project B? If that doesn't work, another option might be to use .Net Reflection.
    Thanks for the reply Bob.

    I added a reference in project B, to project A.

    Same error.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    OK. Well, let's try .Net reflection. I've never attempted Reflection across projects. So, I guess we'll find out if it works or not.

    Project A should look similar to the following:
    Code:
    // Project A
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ProjectB;
    
    namespace ReflectionTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                ProjectBForm1 obj = new ProjectBForm1();
                obj.ProcessRequest(typeof(Program));
            
            }
            public static int A { get; set; }//Getter/Setter is important else "GetProperties" will not be able to detect it
            public static int B { get; set; }
    
        }
    }
    Project B should look similar to the following:
    Code:
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    
    namespace ProjectB
    {
        public partial class ProjectBForm1 : Form
        {
            public ProjectBForm1()
            {
                InitializeComponent();
            }
            public void ProcessRequest(Type cobraType)
            {
                int a, b;
                foreach (var item in cobraType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
                {
                    if (item.Name == "A")
                        a = (int)item.GetValue(null, null);       //Since it is a static class pass null
                    else if (item.Name == "B")
                        b = (int)item.GetValue(null, null);
                }
            }
    
        }
    }
    I would build Project B first, then build Project A and finally added a reference to Project B in Project A.

    Good luck!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    How can I use the infoTxt string variable outside this GlobalVars.cs file, and assign it different strings withing different Methods found in both project A and B?
    [Edit: Add] b.t.w. I've tried accessing that variable in a class file in project B, like this:

    Code:
    GlobalVars.infoTxt = "some text";
    Another option would be to use the fully qualified reference as follows:

    Code:
    A.GlobalVars.infoTxt = "some text";
    since it is in the A namespace but you're calling it from the B namespace.

IMN logo majestic logo threadwatch logo seochat tools logo