我如何知道一个类是否可以用作静态类?
我的课程有几个课程和两种形式。我的第一个表单“Main”有一个按钮将显示第二个表单“formSettings”,还有一个按钮将打开日志文件。
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());
}
}
我的主窗体创建了“设置”窗体和“日志”类的实例。我的设置类还创建 Log 对象的实例。我现在遇到的问题是日志类根据设置类中的条目进行一些验证,但是我无法调用设置类的对象,因为它调用日志类,否则我最终会陷入无限循环。这是它所做的验证。
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();
}
}
我想知道日志文件应该是静态的吗?可以是静态的吗?我希望能够从任一表单调用属于 LogClass 一部分的 LogOpen() 方法,但如上所示,会根据调用日志类的其中一个表单的条目进行一些检查。如果我不能将日志文件用作静态类(我不太了解静态类,因此我提出要求),那么尝试这个问题可能是一个好的解决方案。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,LogClass 绝对可以是静态的,事实上,如果从多个位置访问它,可能会更好。
但是,您需要更改表单设置与日志类的通信方式。您需要向日志类添加静态属性以存储表单中的值,或者需要将表单中的值传递给日志类中的方法。
例如,如果您的用户清除了设置表单中的 chbxLogScanResults,那么您可以立即使用此信息更新 LogClass,或者在保存设置表单时更新 LogClass(我更喜欢在保存时执行此操作,这样如果用户取消对表格,您没有错误地记录偏好)。
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).
静态类只是一个成员都是静态的类。那里没什么特别的。
您应该拥有一个静态的、全局可见的日志记录工具,它只需委托给一个执行实际工作的实例即可。
日志记录代码应该能够使用默认设置,以便在加载设置之前使其能够工作。
最后一点:在
if()
语句之前打开FileStream
和StreamWriter
,并在if 主体内关闭它们()
语句,是一个糟糕的主意。考虑改用using
关键字。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 andStreamWriter
s before anif()
statement, and closing them inside the body of theif()
statement, is a terrible idea. Consider using theusing
keyword instead.