Desktop Application C# Global variable
Desktop app
C#
Compact Framework
2 projects in one solution
Main project uses DataAccess project (reference), because everything related to Database is in DataAcess project I am actually connecting to 2 different databases.
How can I make a "global" variable that would be available from both projects? (without needing to add reference - which, I cannot do, because it would be circular ref)
In web I had (dataaccess part):
string strConnRSS = System.Configuration.ConfigurationManager.AppSettings["something1"];
string strConnRSS = System.Configuration.ConfigurationManager.AppSettings["something2"];
getting data from the web.config (this was in the web prj):
<appSettings>
<add key="something1" value="server=...;database=...;uid=...;password=...;" />
<add key="something2" value="server=...;database=...;uid=...;password=...;" />
</appSettings>
is there something similar in a desktop app?
thanks
Update: http://www.egg开发者_运维百科headcafe.com/articles/dotnetcompactframework_app_config.asp this looks helpful, but we have a settings page, where you can change the actual connection string
Create a 3rd library, with common code for both your projects like this and add references to it.
public static class GlobalVariables
{
public static string SomeCommonVar { get { // read this lib app.config } }
}
Not sure I would like to use this approach. I would always think long and hard before I decide to have a global variable like this, perhaps there is some better design/architecture which would allow you to work around this? Anyway, you did not state which .Net version you are targeting but here is a code sample for 3.5.
In my exe:
private void button1_Click(object sender, EventArgs e)
{
ConfigurationManager.AppSettings.Set("TestData", "TestValue");
string testData = ClassLibrary1.Class1.GetTestData();
if (testData != null )
MessageBox.Show(testData);
else
MessageBox.Show("Not found");
}
In my class library:
public class Class1
{
public static string GetTestData()
{
return ConfigurationSettings.AppSettings["TestData"];
}
}
The exe obviously have a reference to the class library.
精彩评论