The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C# How to declare, and use, solution/project level variables?
Discuss C# How to declare, and use, solution/project level variables? in the C Programming forum on Dev Shed. C# How to declare, and use, solution/project level variables? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 5th, 2012, 04:33 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 101
 
Time spent in forums: 22 h 45 m 20 sec
Reputation Power: 1
|
|
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.
|

December 5th, 2012, 06:11 PM
|
|
|
|
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.
|

December 5th, 2012, 06:42 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 101
 
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.
|

December 5th, 2012, 09:45 PM
|
|
|
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!
|

December 6th, 2012, 04:46 PM
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|