简体   繁体   English

在C#中设置对象的默认`String`值

[英]Setting the default `String` value of an object in C#

I'm relatively new to C#, so the concept of default values is somewhat confusing to me. 我对C#比较新,所以默认值的概念对我来说有点混乱。

I currently have an object which contains two String values, serialNumber and fullPath . 我目前有一个包含两个String值的对象, serialNumberfullPath It's a simple object containing nothing but these two Strings and respective getter methods (they are not to be changed after Object creation; hence the absence of setter methods). 这是一个简单的对象,只包含这两个字符串和相应的getter方法(在创建对象后不会更改它们;因此没有setter方法)。

I want to put a List of these objects into a CheckedListBox . 我想将这些对象的List放入CheckedListBox I want the box to display only the serial number and not the full path. 我希望盒子只显示序列号,而不是完整路径。 According to MSDN , the CheckedListBox uses the default String value of the object. 根据MSDNCheckedListBox使用对象的默认String值。 How can I set this to serialNumber when the object is created? 如何在创建对象时将其设置为serialNumber Again, it should not be changed afterwards. 同样,之后不应该改变它。 Also, I'm not a big fan of the get and set keywords (I come from a Java background), so if if possible, I'd like to do this with more conventional getters/setters as I've done below for purposes of code readability. 另外,我不是getset关键字的忠实粉丝(我来自Java背景),所以如果可能的话,我想用更常规的getter / setter这样做,就像我在下面为了目的所做的那样代码可读性

class ModuleData
{

  private String serialNumber;
  private String fullPath;


  public ModuleData(String serialNumber, String fullPath)
  {
     this.serialNumber = serialNumber;
     this.fullPath = fullPath;
  }

  public String getSerialNumber()
  {
     return serialNumber;
  }

  public String getFullPath()
  {
     return fullPath;
  }

  public String toString()
  {
     return serialNumber;
  }

  public String DefaultValue
  {
     return serialNumber;
  }

}

string or String 's default value, default(string) , is null , but that's not what the documentation refers to. stringString的默认值default(string)null ,但这不是文档引用的内容。 It refers to the object's ToString() value, which you can override . 它引用了对象的ToString()值, 您可以覆盖它

Try something like this, using real C# conventions (look how readable it is!), also notice the override keyword : 尝试这样的东西,使用真正的 C#约定(看看它的可读性!),还要注意override关键字

public class ModuleData
{
    public ModuleData(string serialNumber, string fullPath)
    {
        SerialNumber = serialNumber;
        FullPath = fullPath;
    }

    public string SerialNumber { get; private set; }

    public string FullPath { get; private set; }

    public override string ToString()
    {
        return SerialNumber;
    }
}

To get the string representation of a class instance you override the ToString method returning your desidered value. 要获取类实例的字符串表示形式,请重写ToString方法,以返回所需的值。 In your code you write toString() (lowercase T) and thus being C# case sensitive it is not the correct override. 在您的代码中,您编写toString()(小写T),因此C#区分大小写它不是正确的覆盖。

Just change to 只需改为

 public override string ToString()
 {
     return serialNumber;
 }

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

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