0

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)
         {
         }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    `Freight` is a class, not an instance of a class, what do you expect `Freight.ToString()` to do? – DavidG May 13 '18 at 17:48
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 13 '18 at 17:53
  • It's not a 'null reference' duplicate. The problem is that you call `Freight.ToString()` as a method on a static class, and confuse the compiler. It tries to resolve it to `object.ToString()` and fails. –  May 13 '18 at 17:56
  • You seem to not have properly grasped the concept of classes and instances of classes. You cannot call Freight.ToString() because Freight is a class and ToString is NOT a static method of the Freight class. You need an object reference to access non-static members of classes. see https://stackoverflow.com/q/498400/1800515 – PrashanD May 13 '18 at 17:57
  • Possible duplicate of [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – PrashanD May 13 '18 at 18:00
  • If your static class _really_ needs to have a static `.ToString`, mark it with `new public`, to explicitly hide the one inherited from `object`. –  May 13 '18 at 18:00
  • @DavidG I am trying to use the ToString of another class in this one.I mean I overrided the ToString of class Freight and i want to add it to this one so i can print it like text – Victor Nikolov May 13 '18 at 18:01
  • You can't **add** it to class Freight. I think you meant call or invoke? – PrashanD May 13 '18 at 18:03

2 Answers2

0

To be able to call Freight.ToString() ToString() has to be a static function, i.e., 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

Tarek
  • 1,219
  • 13
  • 18
0

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.