C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2012, 04:33 PM
we5inelgr we5inelgr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 101 we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 22 h 45 m 20 sec
Reputation Power: 1
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.

Reply With Quote
  #2  
Old December 5th, 2012, 06:11 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
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.

Reply With Quote
  #3  
Old December 5th, 2012, 06:42 PM
we5inelgr we5inelgr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 101 we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 22 h 45 m 20 sec
Reputation Power: 1
Quote:
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.

Reply With Quote
  #4  
Old December 5th, 2012, 09:45 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
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!

Reply With Quote
  #5  
Old December 6th, 2012, 04:46 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C# How to declare, and use, solution/project level variables?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap