Reputation: 2943
My program has a handful of classes, and 2 forms. My first form "Main" has a button that will show the second form "formSettings" and a button that will open a log file.
FormSettings formSettings = new FormSettings();
LogClass objectLog = new LogClass();
public void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
//shows the settings form
formSettings.Show();
}
private void viewLogToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
objectLog.OpenLogFile();
}
catch (Exception ee)
{
objectMessageBox.ReturnErrorOpeningLogPrompt(ee.ToString());
}
}
My Main form creates instances of both the Settings form as well as the Log class. My settings class also creates an instance of the Log object. The problem I have now is that the log class does some validating based on entries from the settings class, however I can't call for an object of the settings class since it calls the log class or I end up with an infinite loop. Here's the validation it does.
public void Write_Log_Data(string data)
{
//Create an outfile stream
FileStream outfile = new FileStream(fileLocation,
FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(outfile);
if (objectSettings.chbxLogScanResults.Checked == true)
{
if (data == null || data == "")
{
//this is for logging ip addresses
writer.WriteLine(Properties.Settings.Default.IPAddressNew + CONST_TAB +
GetDateTime());
}
//because logs containing errors or changed ips are not null data they trigger this section
else
{
//the error was already formatted so just write it
writer.WriteLine(data);
}
//close our writers
writer.Close();
outfile.Close();
}
}
What I'm wondering is should the log file be static? Can it be static? I want to be able to call a LogOpen() method that is part of the LogClass from either form, but as shown above there is some checking that occurs based on entries of one of the forms that calls the log class. What might be a good solution to try for this if I can't use the log file as a static class (I don't understand static classes very well hence my asking).
Upvotes: 3
Views: 102
Reputation: 61986
A static class is just a class whose members are all static. Nothing special there.
You should have a static, globally visible logging facility which simply delegates to an instanced one which does the real job.
The logging code should be able to work with default settings, so as to enable it to work even before the settings have been loaded.
One final note: opening FileStream
s and StreamWriter
s before an if()
statement, and closing them inside the body of the if()
statement, is a terrible idea. Consider using the using
keyword instead.
Upvotes: 2
Reputation: 44931
Yes, LogClass can absolutely be static and, in fact, if it is accessed from multiple locations, it is probably better that it is.
However, you will need to change how the form settings are communicated to the log class. You will either need to add static properties to the log class to store the values from the forms or you will need to pass the values from the forms to the methods in the log class.
For example, if your user clears the chbxLogScanResults in your settings form, then you can either update the LogClass with this information immediately or when the settings form is saved (I prefer to do this on save so that if the user cancels the changes to the form, you don't record preferences incorrectly).
Upvotes: 2