5

I'm relatively new to C#, so the concept of default values is somewhat confusing to me.

I currently have an object which contains two String values, serialNumber and fullPath. 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).

I want to put a List of these objects into a 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. How can I set this to serialNumber when the object is created? 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.

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;
  }

}
11
  • 4
    It is ToString(), capital T Commented Jun 25, 2014 at 12:47
  • 5
    Using the conventions from a different language results in pain in the long run. Don't write Java in C#. They're two different languages. Commented Jun 25, 2014 at 12:48
  • 1
    @Steve Perfect, it works like a charm, much appreciated. Please make your comment an answer so I can give you proper credit. Commented Jun 25, 2014 at 12:49
  • 3
    @audiFanatic that shouldn't "work like a charm" - it is missing a very important piece: override Commented Jun 25, 2014 at 12:51
  • 2
    It's a valid question, the documentation refers to "the default string", without explaining what that is. Commented Jun 25, 2014 at 12:51

2 Answers 2

10

string or String's default value, default(string), is null, but that's not what the documentation refers to. It refers to the object's ToString() value, which you can override.

Try something like this, using real C# conventions (look how readable it is!), also notice the override keyword:

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, good catch with the override keyword. VS yelled at me when I forgot it
1

To get the string representation of a class instance you override the ToString method returning your desidered value. In your code you write toString() (lowercase T) and thus being C# case sensitive it is not the correct override.

Just change to

 public override string ToString()
 {
     return serialNumber;
 }

1 Comment

Had to choose khellang as he included the override keyword first, hope you don't take offense to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.