简体   繁体   中英

Error with using ToString in ToString override

I am new to coding and I have a problem with using ToString override. When I try to use already overloaded class's ToString in other ToString override, I get this error:

An object reference is required to non static field, method, or property Freight.ToString()

Please help!

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace TruckCmopany
 {
     class Truck
     {
         private string name;
         private double weightCapacity;
         private List<Freight> freights;

         public Truck(string name,double weightCapacity)
         {
              this.Name = name;
              this.WeightCapacity = weightCapacity;
              List<Freight> freights = new List<Freight>();
         }

         public string Name
         {
             get { return name; }
             set { name = value; }
         }

         public double WeightCapacity
         {
             get { return weightCapacity; }
             set { weightCapacity = value; }
         }

         public override string ToString()
         {
             StringBuilder sb = new StringBuilder();
             sb.Append(this.Name).Append(" - ");

             if (freights.Count==0)
             {
                 sb.Append("Nothing loaded");
             }
             else
             {
                 sb.Append(string.Join(", ", freights)).Append(Freight.ToString());
             }

             return sb.ToString();
         }

         public IReadOnlyCollection<Freight> Freights
         {
             get => freights.AsReadOnly();
         }

         public void AddFreight(Freight freight)
         {
         }
    }
}

To be able to call Freight.ToString() ToString() has to be a static function, ie, a function that belongs to the class itself that doesn't require any specific instance of the class to get invoked. However when you convert ToString to be static, you won't have access to the this modifier, since in a static method, there is no instance that you can refer to with this . So you are going to have to pass an instance of Freight to your static function.

You can then override ToString() and inside pass this to your static function. If you want to.

Read on Static Members: MSDN

Read on Methods: MSDN

Freight.ToString() is a call to a static method. It confuses the compiler, because it tries to resolve the call to the non-static .ToString inherited from object .

Since static classes cannot override members, if you really need a static ToString in your Freight class, you need to mark the method with new public .


Well, I see that your Freight is not a static class.

Then if I got you right, and you want a comma-separated string of freights, you need to simply do

sb.Append(string.Join(", ", freights));

It will call .ToString for each element implicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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