C# Theory question
-
I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.
-
I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.
.gonad wrote: i have a "Tools > Options" dialog box You have or want? .gonad wrote: i want the whole application to reference these settings for the database connection. Add a name/value to your config file. Then you can just get the value via dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
.gonad wrote: i have a "Tools > Options" dialog box You have or want? .gonad wrote: i want the whole application to reference these settings for the database connection. Add a name/value to your config file. Then you can just get the value via dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
Hi. actually, I already have a dialog box where the user will have to enter the db connection properties. basically, i wrote this because i use this app at home AND at the office, but my network settings are different, so this "tools > options" dialog box will be helpful for me to make the these changes easily. I just don't know how to make those changes permanent throughout the execution of the app and how to call those settings from each of the forms. Thanks for you help. :)
-
I'm new to c# and programming altogether, so can you help me figure out the best way to do this? i have a "Tools > Options" dialog box where a user can put in the db settings to connect to a database (IP, username, password, etc). once a user submits these settings, i want the whole application to reference these settings for the database connection. i have about 5 forms on this small app that connect to a db, so i don't want to statically assign these settings per form. can i (or should I) use a struct and define the users settings as constants? if so, how can i call the struct from each form? Thanks for your help.
The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like:
public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; }
Notice the keywordstatic
infront. This means that you do not need an instance of the class to reference those members. You would access them like:MySettings.MyDbConnectionString = ...
So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature? -
The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like:
public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; }
Notice the keywordstatic
infront. This means that you do not need an instance of the class to reference those members. You would access them like:MySettings.MyDbConnectionString = ...
So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature? -
The best way to make the settings global to your application is by having a class that is visible to the whole app. Something like:
public class MySettings { public static string MyDbConnectionString; public static string SomeOtherSettings; }
Notice the keywordstatic
infront. This means that you do not need an instance of the class to reference those members. You would access them like:MySettings.MyDbConnectionString = ...
So you just load your settings from a file at the start of your app, and then just set them again from the options dialog. Hope this helps and gives you a start, Nathan --------------------------- Hmmm... what's a signature?Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. That data will be available to all the applications running on the machine via Dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. That data will be available to all the applications running on the machine via Dynamic properties. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
leppie wrote: Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. I would advise against this for a couple reasons:
- Your app will break if you move to a new version of the framework, but forget to update the machine.config for that version of the framework. Or worse, another application does the same thing, but storing a different connection string.
- You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it.
Instead you should store the read-only setting in the app.config file. If you expect to make more applications using the same connection string then you would be better off creating your own settings class and storing that in the user's application directory, or the common application directory if the settings should apply to everyone. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him
-
Thank you for your help. that's exactly what i needed to know, and it works like i expected. :-O
Glad that I could be of help :) --------------------------- Hmmm... what's a signature?
-
leppie wrote: Its generally better to place things like your own SQL/Access connectiong string in the machine.config file. I would advise against this for a couple reasons:
- Your app will break if you move to a new version of the framework, but forget to update the machine.config for that version of the framework. Or worse, another application does the same thing, but storing a different connection string.
- You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it.
Instead you should store the read-only setting in the app.config file. If you expect to make more applications using the same connection string then you would be better off creating your own settings class and storing that in the user's application directory, or the common application directory if the settings should apply to everyone. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him
James T. Johnson wrote: You are taking something that should be local, and making it global. You wouldn't want your connection strings entered into another application so you should keep them local to the apps that need it. I realise that! I was thinking more in a development enviroment, rather than production. Obviously on deployment, an app.config file is created to carry the setting. That makes sense doesnt it? leppie::AllocCPArticle(Generic DFA State Machine for .NET);