I have looked up all the "more efficient" posts on here and none answers this directly, so I need some big brain to help me out.
I currently store settings in app.config and get the values as I need them. I use this method:
public static string AppConfig_GetKeyValue(string strAppSettingKeyName)
{
ConfigurationManager.RefreshSection("appSettings");
string myAppSettingValue = ConfigurationManager.AppSettings[strAppSettingKeyName];
return myAppSettingValue;
}
The QUESTION is ... if I'm doing a ton of operations, like SQL inserts or writing info to the screen or terminal, is it better to store that value globally for speed/efficiency? For example:
If on EVERY SQL insert, we check the app.config for the value:
<add key="mySQLLogging" value="true"/>
Versus declaring a global like:
public static bool mySQLLoggingOn = bool.Parse(TTLog.AppConfig_GetKeyValue("mySQLLogging"));
Which is better? Maybe a brief explanation as to why?
As a second example, I log things to the screen for the user, but include the text DEBUG in each line if I want detailed information, but don't want to show it unless that mode is "true" in app.config:
public static void DoWriteLine(string strMessage, bool blnShowDateStamp = true, ConsoleColor clrForeColor = ConsoleColor.Green, ConsoleColor clrBackColor = ConsoleColor.Black)
{
if (strMessage.ToLower().Contains("debug") && !(bool.Parse(AppConfig_GetKeyValue("myModeDebugOn")))
return; // If app.config key is false, don't process message to the screen
Console.ForegroundColor = clrForeColor;
Console.BackgroundColor = clrBackColor;
if (blnShowDateStamp)
Console.WriteLine(("[ " + DateTime.Now.ToString("ddd MM/dd/yyyy hh:mm:ss.fff") + " ] -> " + strMessage).PadRight(Console.WindowWidth - 1));
else
Console.WriteLine((strMessage).PadRight(Console.WindowWidth - 1));
Console.ResetColor();
}
The above, obviously, corresponds to the key in the app.config:
<add key="myModeDebugOn" value="True"/>
Which then, potentially every second, has to process this:
if (strMessage.ToLower().Contains("debug") && !(bool.Parse(AppConfig_GetKeyValue("myModeDebugOn")))
return;
So are all these file read operations WAY less efficient? I just remember from day one of learning programming, NEVER use global variables, it's bad.
Thanks!