一次写一条记录,使用一个静态类?(Writing one record at a time, use one static class? [closed])

每次按下按钮时,我都会在文件中写入一行文本。

我已经定义了一个这样的静态类:

public static class PalletRecord { public static string MachineName { get; set; } public static DateTime TimeStamp { get; set; } public static string BranchPlant { get; set; } public static string Location { get; set; } public static string Data { get; set; } public static string ItemCode { get; set; } public static string ItemDescription { get; set; } public static decimal Quantity { get; set; } }

每次我需要写入文件我填充静态类属性,我使用File.AppendAllLines写入文本文件。

我应该用非静态类来完成它吗?

I am writing one line of text in a file every time a button is pressed.

I have defined a static class like this:

public static class PalletRecord { public static string MachineName { get; set; } public static DateTime TimeStamp { get; set; } public static string BranchPlant { get; set; } public static string Location { get; set; } public static string Data { get; set; } public static string ItemCode { get; set; } public static string ItemDescription { get; set; } public static decimal Quantity { get; set; } }

Every time i need to write to a file i populate the static class properties and i use File.AppendAllLines to write to text file.

Should i have done it with a non-static class?

最满意答案

是的! 使用静态来保持这样的状态会给你带来麻烦。 如果现在或将来需要多线程,这可能尤其如此,因为静态状态是所有线程共享的。 另一个问题可能是一个不完整的初始化错误,让你从之前的记录中获得1/2的状态,从新的记录中获得1/2的状态。

更好的方法是将属性设置为非静态,并为每个记录创建具有正确属性的类的一个实例。 你可以创建一个构造函数来设置属性,这样当你有一个实例时,你就知道你有一个有效的记录(也可以考虑使属性设置器保持私有状态)。

这也能更好地模拟您正在使用的问题。 您在文件中编写多个不同的记录,这最好由表示记录的类的实例来表示。 在我看来,为每个需要编写的记录使用类的实例是适当的干净的面向对象的方式。

Yes ! Using statics to keep state like this is going to get you in trouble. This might especially be true if you might need multi threading now or in the future, since the static state is shared by all threads. Another problem could be an incomplete initialization bug, leaving you with ½ the state from a previous record and ½ the state from a new record.

A much better approach would be to make the properties non-static, and create one instance of the class with the correct properties for each record. You could make a constructor which sets the properties, so that you know you have a valid record whenever you have an instance (also consider making the property setters private).

This also better models the problem you are working with. You write multiple, different records in the file, this is best represented by instances of a class that represents a record. In my opinion, using an instance of your class for each record you need to write is the proper clean object oriented way to do this.

一次写一条记录,使用一个静态类?(Writing one record at a time, use one static class? [closed])

每次按下按钮时,我都会在文件中写入一行文本。

我已经定义了一个这样的静态类:

public static class PalletRecord { public static string MachineName { get; set; } public static DateTime TimeStamp { get; set; } public static string BranchPlant { get; set; } public static string Location { get; set; } public static string Data { get; set; } public static string ItemCode { get; set; } public static string ItemDescription { get; set; } public static decimal Quantity { get; set; } }

每次我需要写入文件我填充静态类属性,我使用File.AppendAllLines写入文本文件。

我应该用非静态类来完成它吗?

I am writing one line of text in a file every time a button is pressed.

I have defined a static class like this:

public static class PalletRecord { public static string MachineName { get; set; } public static DateTime TimeStamp { get; set; } public static string BranchPlant { get; set; } public static string Location { get; set; } public static string Data { get; set; } public static string ItemCode { get; set; } public static string ItemDescription { get; set; } public static decimal Quantity { get; set; } }

Every time i need to write to a file i populate the static class properties and i use File.AppendAllLines to write to text file.

Should i have done it with a non-static class?

最满意答案

是的! 使用静态来保持这样的状态会给你带来麻烦。 如果现在或将来需要多线程,这可能尤其如此,因为静态状态是所有线程共享的。 另一个问题可能是一个不完整的初始化错误,让你从之前的记录中获得1/2的状态,从新的记录中获得1/2的状态。

更好的方法是将属性设置为非静态,并为每个记录创建具有正确属性的类的一个实例。 你可以创建一个构造函数来设置属性,这样当你有一个实例时,你就知道你有一个有效的记录(也可以考虑使属性设置器保持私有状态)。

这也能更好地模拟您正在使用的问题。 您在文件中编写多个不同的记录,这最好由表示记录的类的实例来表示。 在我看来,为每个需要编写的记录使用类的实例是适当的干净的面向对象的方式。

Yes ! Using statics to keep state like this is going to get you in trouble. This might especially be true if you might need multi threading now or in the future, since the static state is shared by all threads. Another problem could be an incomplete initialization bug, leaving you with ½ the state from a previous record and ½ the state from a new record.

A much better approach would be to make the properties non-static, and create one instance of the class with the correct properties for each record. You could make a constructor which sets the properties, so that you know you have a valid record whenever you have an instance (also consider making the property setters private).

This also better models the problem you are working with. You write multiple, different records in the file, this is best represented by instances of a class that represents a record. In my opinion, using an instance of your class for each record you need to write is the proper clean object oriented way to do this.