Share via

Error using public override string ToString

Raymond Gilbers 176 Reputation points
2022-07-02T09:50:56.723+00:00

I need to use Public override ToString() two times, how can I do that?

 public override string ToString()  
        {  
            return MachineName ;  //Needed for showing the string into the combobox  
        }  
          
        public override string ToString()  
        {  
            return SerialNumber;  
        }   

This obviously doesn't work

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-07-02T11:05:15.51+00:00

    You can not have ToString more than once in a class.

    See the definition

      [ClassInterface(ClassInterfaceType.AutoDual)]  
      [ComVisible(true)]  
      [__DynamicallyInvokable]  
      [Serializable]  
      public class Object  
      {  
        /// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>  
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]  
        [NonVersionable]  
        [__DynamicallyInvokable]  
        public Object()  
        {  
        }  
      
        /// <summary>Returns a string that represents the current object.</summary>  
        /// <returns>A string that represents the current object.</returns>  
        [__DynamicallyInvokable]  
        public virtual string ToString() => this.GetType().ToString();  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Raymond Gilbers 176 Reputation points
    2022-07-15T11:30:08.86+00:00

    With unit testing you mean testing the code for bugs?


  2. Raymond Gilbers 176 Reputation points
    2022-07-14T20:06:45.847+00:00

    I think I need some training and a bit more advance as just a hello world example. I see I get really good suggestions on questions I ask here but I get stuck at certain terminology and understanding how to apply those suggestions. And looking at the MSDN library I have the same issue.
    The basics I understand but making a program like I'm making at the moment I should need more knowledge and understanding. Nevertheless I'm pretty far with what I wanted to accomplish with the program I made.

    If anybody (if I may ask) knows some good online training please feel free to tell me some of those.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.