Skip to content

Commit

Permalink
Convert String.Format to interpolated strings and remove string conca…
Browse files Browse the repository at this point in the history
…tenation in favour of interpolated strings.
  • Loading branch information
Peter-Simpson committed Jan 15, 2025
1 parent fc8be0e commit 09c5cd0
Show file tree
Hide file tree
Showing 12 changed files with 6,921 additions and 239 deletions.
16 changes: 8 additions & 8 deletions Remote Server/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Configuration()

public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
{
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Getting {0} value '{1}' in subkey '{2}', default: '{3}'", typeof(T).Name, KeyName, SubKey, DefaultValue.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", $"Getting {typeof(T).Name} value '{KeyName}' in subkey '{SubKey}', default: '{DefaultValue}'");

if (typeof(T) == typeof(bool))
{
Expand All @@ -66,7 +66,7 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
}

bool RetVal = Convert.ToBoolean(registryValue, CultureInfo.InvariantCulture);
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", $"Retrieved {KeyName} = {RetVal}");
return (T)((object)RetVal);
}

Expand All @@ -91,7 +91,7 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
SetValue<T>(KeyName, SubKey, DefaultValue);
RetVal = DefaultValue.ToString();
}
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", $"Retrieved {KeyName} = {RetVal}");
return (T)((object)RetVal);
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
}

decimal RetVal = Convert.ToDecimal(registryValue, CultureInfo.InvariantCulture);
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", $"Retrieved {KeyName} = {RetVal}");
return (T)((object)RetVal);
}

Expand Down Expand Up @@ -150,7 +150,7 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
if (DateTime.TryParse(registryValue, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out DateTime RetVal))
{
// The string parsed OK so return the parsed value;
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue DateTime", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue DateTime", $"Retrieved {KeyName} = {RetVal}");
return (T)((object)RetVal);
}
else // If the string fails to parse, overwrite with the default value and return this
Expand Down Expand Up @@ -185,7 +185,7 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)
}

int RetVal = Convert.ToInt32(registryValue, CultureInfo.InvariantCulture);
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "GetValue", $"Retrieved {KeyName} = {RetVal}");
return (T)((object)RetVal);
}

Expand All @@ -194,15 +194,15 @@ public T GetValue<T>(string KeyName, string SubKey, T DefaultValue)

public void SetValue<T>(string KeyName, string SubKey, T Value)
{
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "SetValue", string.Format("Setting {0} value '{1}' in subkey '{2}' to: '{3}'", typeof(T).Name, KeyName, SubKey, Value.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "SetValue", $"Setting {typeof(T).Name} value '{KeyName}' in subkey '{SubKey}' to: '{Value}'");

if (SubKey == "") baseRegistryKey.SetValue(KeyName, Value.ToString());
else baseRegistryKey.CreateSubKey(SubKey).SetValue(KeyName, Value.ToString());
}

public void SetValueInvariant<T>(string KeyName, string SubKey, T Value)
{
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "SetValue DateTime", string.Format("Setting {0} value '{1}' in subkey '{2}' to: '{3}'", typeof(T).Name, KeyName, SubKey, Value.ToString()));
if (LOG_CONFIGURATION_CALLS) ServerForm.LogMessage(0, 0, 0, "SetValue DateTime", $"Setting {typeof(T).Name} value '{KeyName}' in subkey '{SubKey}' to: '{Value}'");

if ((typeof(T) == typeof(Int32)) | (typeof(T) == typeof(int)))
{
Expand Down
2 changes: 1 addition & 1 deletion Remote Server/ConfiguredDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public string DeviceKey
{
get
{
return string.Format("{0}/{1}", DeviceType.ToLowerInvariant(), DeviceNumber); // Create the device key
return $"{DeviceType.ToLowerInvariant()}/{DeviceNumber}"; // Create the device key
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Remote Server/DriverHostForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public DriverHostForm(KeyValuePair<string, ConfiguredDevice> device, ServerForm
/// <param name="e"></param>
private void DriverHostForm_Load(object sender, EventArgs e)
{
deviceKey = string.Format("{0}/{1}", configuredDevice.Value.DeviceType.ToLowerInvariant(), configuredDevice.Value.DeviceNumber);
deviceKey = $"{configuredDevice.Value.DeviceType.ToLowerInvariant()}/{configuredDevice.Value.DeviceNumber}";

ServerForm.LogMessage(0, 0, 0, "DriverHostForm", $"Creating driver {deviceKey} ({configuredDevice.Key}) on thread {Environment.CurrentManagedThreadId} with apartment state {Thread.CurrentThread.GetApartmentState()}");
restServer.CreateInstance(configuredDevice); // Create the driver on this thread
ServerForm.LogMessage(0, 0, 0, "DriverHostForm", string.Format("Created driver {0} ({1}) on thread {2}", deviceKey, configuredDevice.Key, Environment.CurrentManagedThreadId));
ServerForm.LogMessage(0, 0, 0, "DriverHostForm", $"Created driver {deviceKey} ({configuredDevice.Key}) on thread {Environment.CurrentManagedThreadId}");

ServerForm.ActiveObjects[deviceKey].DriverHostForm = this; // Save the driver host form reference so that calls can be made to the driver
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public Thread DriverCommand(RequestData requestData)
catch (Exception ex) // Something serious has gone wrong with the ASCOM Remote server itself so report this to the user
{
ServerForm.LogException1(requestData, "DriverCommand", ex.ToString());
restServer.Return500Error(requestData, "Internal server error (DriverOnSeparateThread): " + ex.ToString());
restServer.Return500Error(requestData, $"Internal server error (DriverOnSeparateThread): {ex}");
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion Remote Server/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static string ToCorrectCase(this string methodName, string deviceType = n
}
catch (Exception ex)
{
return $"UnknownMethod:'{lookupKey}' - {ex.ToString()}";
return $"UnknownMethod:'{lookupKey}' - {ex}";
}
}

Expand Down
4 changes: 2 additions & 2 deletions Remote Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static void Application_ThreadException(object sender, ThreadExceptionEventArgs
{
Enabled = true
};
TL.LogMessage("Main", "Thread exception: " + e.Exception.ToString());
TL.LogMessage("Main", $"Thread exception: {e.Exception}");
Process.Start(TL.LogFileName);

TL.Enabled = false;
Expand All @@ -165,7 +165,7 @@ static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEv
{
Enabled = true
};
TL.LogMessage("Main", "Un-handled exception: " + exception.ToString());
TL.LogMessage("Main", $"Un-handled exception: {exception}");
Process.Start(TL.LogFileName);
TL.Enabled = false;
TL.Dispose();
Expand Down
23 changes: 6 additions & 17 deletions Remote Server/ServedDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,14 @@ public void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
private void BtnSetup_Click(object sender, EventArgs e)
{
// This device's ProgID is held in the variable progID so try and run its SetupDialog method
ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Setup button pressed for device: {0}, ProgID: {1}", cmbDevice.Text, progID));
ServerForm.LogMessage(0, 0, 0, "Setup", $"Setup button pressed for device: {cmbDevice.Text}, ProgID: {progID}");

try
{
// Get an instance of the driver from its ProgID and store this in a dynamic variable so that we can call its method directly
Type ProgIdType = Type.GetTypeFromProgID(progID);
//ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Found type: {0}", ProgIdType.Name));

dynamic oDrv = Activator.CreateInstance(ProgIdType);
//ServerForm.LogMessage(0, 0, 0, "Setup", "Created driver instance OK");

try
{
Expand All @@ -348,19 +346,15 @@ private void BtnSetup_Click(object sender, EventArgs e)
DialogResult dialogResult = MessageBox.Show("Device is connected, OK to disconnect and run Setup?", "Disconnect Device?", MessageBoxButtons.OKCancel);
if (dialogResult == DialogResult.OK) // OK to disconnect and run setup dialogue
{
//ServerForm.LogMessage(0, 0, 0, "Setup", "User gave permission to disconnect device - setting Connected to false");
try { oDrv.Connected = false; } catch { }; // Set Connected to false ignoring errors
try { oDrv.Link = false; } catch { }; // Set Link to false (for IFocuserV1 devices) ignoring errors

int RemainingObjectCount = Marshal.FinalReleaseComObject(oDrv);
int RemainingObjectCount = Marshal.FinalReleaseComObject(oDrv);

oDrv = null;
oDrv = Activator.CreateInstance(ProgIdType);

//ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Connected has bee set false and destroyed. New Connected value: {0}", oDrv.Connected));

//ServerForm.LogMessage(0, 0, 0, "Setup", "Device is now disconnected, calling SetupDialog method");
oDrv.SetupDialog();
//ServerForm.LogMessage(0, 0, 0, "Setup", "Completed SetupDialog method, setting Connected to true");

try
{
Expand All @@ -372,8 +366,6 @@ private void BtnSetup_Click(object sender, EventArgs e)
ServerForm.LogException(0, 0, 0, "Setup", $"Error setting Connected to true for focuser device {ProgID}, now trying Link for IFocuserV1 devices: \r\n{ex2}");
oDrv.Link = true;
}

//ServerForm.LogMessage(0, 0, 0, "Setup", "Driver is now Connected");
}
else // Not OK to disconnect so just do nothing and exit
{
Expand All @@ -382,9 +374,7 @@ private void BtnSetup_Click(object sender, EventArgs e)
}
else // Driver is not connected
{
//ServerForm.LogMessage(0, 0, 0, "Setup", "Device is disconnected so just calling SetupDialog method");
oDrv.SetupDialog();
//ServerForm.LogMessage(0, 0, 0, "Setup", "Completed SetupDialog method");

try { oDrv.Dispose(); } catch { }; // Dispose the driver if possible

Expand All @@ -399,20 +389,19 @@ private void BtnSetup_Click(object sender, EventArgs e)
{
LoopCount += 1; // Increment the loop counter so that we don't go on for ever!
RemainingObjectCount = Marshal.ReleaseComObject(oDrv);
//ServerForm.LogMessage(0, 0, 0, "Setup", " Remaining object count: " + RemainingObjectCount.ToString() + ", LoopCount: " + LoopCount);
} while ((RemainingObjectCount > 0) & (LoopCount < 20));
}
catch (Exception ex2)
{
ServerForm.LogMessage(0, 0, 0, "Setup", " ReleaseComObject Exception: " + ex2.Message);
ServerForm.LogMessage(0, 0, 0, "Setup", $" ReleaseComObject Exception: {ex2.Message}");
}

oDrv = null;
}
}
catch (Exception ex1)
{
string errMsg = string.Format("Exception calling SetupDialog method: {0}", ex1.Message);
string errMsg = $"Exception calling SetupDialog method: {ex1.Message}";
MessageBox.Show(errMsg);
ServerForm.LogMessage(0, 0, 0, "Setup", errMsg);
ServerForm.LogException(0, 0, 0, "Setup", ex1.ToString());
Expand All @@ -421,7 +410,7 @@ private void BtnSetup_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
string errMsg = string.Format("Exception creating driver {0} - {1}", progID, ex.Message);
string errMsg = $"Exception creating driver {progID} - {ex.Message}";
MessageBox.Show(errMsg);
ServerForm.LogMessage(0, 0, 0, "Setup", errMsg);
ServerForm.LogException(0, 0, 0, "Setup", ex.ToString());
Expand Down
Loading

0 comments on commit 09c5cd0

Please sign in to comment.