简体   繁体   English

将不同的字符串添加到同一列表中的对象的正确方法是什么?

[英]What is the proper way to add different strings to objects in same list?

Following example does not represent the actual class and issue, it has been adapted for various reasons. 以下示例不代表实际的类别和问题,由于各种原因已对其进行了修改。

I have a List<EnginePart> populated with objects containing byte KeyID, int Length, byte[] Value . 我有一个List<EnginePart>填充了包含byte KeyID, int Length, byte[] Value Each object was created sequentially and has its own meaning. 每个对象都是按顺序创建的,具有自己的含义。 The list represents "parts to be replaced" and we want to output that to a sales person in a nice format, so he can tell that to the customer. 该列表代表“要更换的零件”,我们希望以一种很好的格式将其输出给销售人员,以便他可以将其告知客户。 Each part has its own id, lets say 0x20 - cylinder, 0x40 - oil filter. 每个零件都有自己的编号,例如0x20-气缸,0x40-机油滤清器。

Now i'd like to add/display a human readable string to each object in a nice way, without iterating through a foreach and checking 现在,我想以一种不错的方式向每个对象添加/显示一个人类可读的字符串,而无需遍历foreach和检查

if(enginePart.key =="0x20") Console.WriteLine("Cylinder rotation count is " + enginePart.value) . if(enginePart.key =="0x20") Console.WriteLine("Cylinder rotation count is " + enginePart.value)

if(enginePart.key =="0x40") Console.WriteLine("Oil filter only filters " + enginePart.value + " of oil) . if(enginePart.key =="0x40") Console.WriteLine("Oil filter only filters " + enginePart.value + " of oil)

Is there any other, nicer way to do it? 还有其他更好的方法吗? Creating a new class for each engine part is not a possibility. 不可能为每个引擎零件创建新的类。

So far I've come up with 3 possible solutions; 到目前为止,我已经提出了3种可能的解决方案。

1) Iterate through the list and have a bunch of ifs and WriteLines 1)遍历列表,并有一堆ifs和WriteLines

2) Add a string to the object on creation, but in that case we still have if statements 2)在创建对象时添加一个字符串,但是在这种情况下,我们仍然具有if语句

3) Create some enum and use that on object creation 3)创建一些枚举并将其用于对象创建

First I'd suggest you to override the TestClass.ToString() Method, so you won't have to build the string describing the current object in your loop. 首先,我建议您重写TestClass.ToString()方法,这样就不必构建描述循环中当前对象的字符串。

See https://docs.microsoft.com/dotnet/api/system.object.tostring?view=netframework-4.7.2 请参阅https://docs.microsoft.com/dotnet/api/system.object.tostring?view=netframework-4.7.2

Then maybe the 2nd solution would be quite okay, although we'd need more details on what your goal really is, or have the content of your classes. 然后,也许第二种解决方案就可以了,尽管我们需要更多有关您的目标是什么或类的内容的详细信息。

Usually when you´re after a "human readable string-representation" you should overwrite ToString : 通常,当您使用“人类可读的字符串表示形式”时,应该覆盖ToString

class EnginePart
{
    byte KeyID { get; set; }
    int Length { get; set; }
    byte[] Value { get; set; }

    public override string ToString()
    {
        return KeyId == "0x40" ? "Oil filter only filters " + value :
               KeyId == "0x20" ? "Cylinder rotation count is " + value :
               ...
    }
}

This way you can rely on the class´ own implementation instead of creating your own in client-code. 这样,您可以依靠类自己的实现,而不用用客户端代码创建自己的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM