global settings
-
Hi I have a big solution which contains many projects. I created a class public class MySettingClass:ApplicationSettingsBase which contains a few settings. I would like to have an access to these variables from almost all projects. How to do that in the most efficient way? When I want to use any setting variable I create an object public MySettingClass mySettings = new MySettingClass(); how to make it visible in all projects in solution? I do not want to pass any parameters (there would be too many changes). Do I have to create an object in each project? No sense. Thanks for help Ela
-
Hi I have a big solution which contains many projects. I created a class public class MySettingClass:ApplicationSettingsBase which contains a few settings. I would like to have an access to these variables from almost all projects. How to do that in the most efficient way? When I want to use any setting variable I create an object public MySettingClass mySettings = new MySettingClass(); how to make it visible in all projects in solution? I do not want to pass any parameters (there would be too many changes). Do I have to create an object in each project? No sense. Thanks for help Ela
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
I thought so... Thanks for confirmation. I wondered if there is another way but I suppose not. Ela
e_LA wrote:
Thanks for confirmation. I wondered if there is another way but I suppose not.
I just confirmed what I tend to do, and that has fit every situation that I've needed so far. There may be other ways - What kind of thing were you looking for?
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
e_LA wrote:
Thanks for confirmation. I wondered if there is another way but I suppose not.
I just confirmed what I tend to do, and that has fit every situation that I've needed so far. There may be other ways - What kind of thing were you looking for?
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
Colin, Could you provide me with any example for such a specific project? I have one class e.g. public class MyAppSettings : ApplicationSettingsBase { [UserScopedSetting()] [DefaultSettingValueAttribute("true")] [global::System.Configuration.ApplicationScopedSettingAttribute()] public bool someProperty { get { return (bool)this["someProperty"]; } set { this["someProperty"] = value; } } Then I have to create a project dealing with the setting. I suppose it should contain class with static properties?? Could you give me an example? Thanks Ela
-
You could create a specific project for dealing with settings and then reference it from all the projects that need it. You can then access the classes from anywhere in your solution (just remember to include the namespace).
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
You can create one object only. (The class exists in a single project and it can use the singleton pattern so that only one instance of the object exists across the whole application)
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
I think do not undrestand everything. I have many projects in solution and in almost all of them I need see global varialbes. Do I have to add a references to my "global" class with settings and create an object. There is something wrong. Ela
e_LA wrote:
Do I have to add a references to my "global" class with settings and create an object.
Every time you use your settings class you will need to include the namespace and reference the project that the in which the settings class resides. i.e. In the project that you wish to use the settings class you "Add Reference..." to the project that contains the settings class.
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Colin, Could you provide me with any example for such a specific project? I have one class e.g. public class MyAppSettings : ApplicationSettingsBase { [UserScopedSetting()] [DefaultSettingValueAttribute("true")] [global::System.Configuration.ApplicationScopedSettingAttribute()] public bool someProperty { get { return (bool)this["someProperty"]; } set { this["someProperty"] = value; } } Then I have to create a project dealing with the setting. I suppose it should contain class with static properties?? Could you give me an example? Thanks Ela
e_LA wrote:
Then I have to create a project dealing with the setting. I suppose it should contain class with static properties??
You can use a static class. Although I prefer to use the Singleton pattern because I can mock the class for unit testing.
public static MySettingsClass
{
public bool static SomeProperty
{
get
{
return this.someProperty
}
}
}My settings classes never have setters. They are readonly.
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
e_LA wrote:
but where to create the object of settings class?
If you are using the singleton pattern then the class itself creates the object. For example:
public Settings
{
// Field containing the only instance of the class
private static Settings onlyInstance;// Private constructor so that nothing outside this class // can construct it. private Settings(){}; // Property getter to return the only ever instance of the class public static Instance { get { return this.onlyInstance; } } // Add your public methods and properties here
}
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
e_LA wrote:
but where to create the object of settings class?
If you are using the singleton pattern then the class itself creates the object. For example:
public Settings
{
// Field containing the only instance of the class
private static Settings onlyInstance;// Private constructor so that nothing outside this class // can construct it. private Settings(){}; // Property getter to return the only ever instance of the class public static Instance { get { return this.onlyInstance; } } // Add your public methods and properties here
}
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
Colin, When I was waiting for the answer I managed to do that in such a way you explained and it works! I can make a few changes after reading you responses. Many thanks for the explanation! ...very strange this singleton pattern :-) I had problems with saving settings but I created public method in singleton and called settings save method. Redards Ela