none
Problem with using ToString in ToString override RRS feed

  • Question

  • Hello,

    i am new in 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 am hitting this error : "An object reference is required to non static field,method,or property Freight.ToString()".

    Please help!!!


    Sunday, May 13, 2018 5:55 PM

All replies

  • Give some code to us.
    Sunday, May 13, 2018 6:05 PM
  • 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)
            {

            }

        }
    }
    Sunday, May 13, 2018 6:11 PM
  • You use Freight but it is type. I think you want to use Freights property not Freight type.

    But I think it will not work because of ToString() will be called at IReadOnlyCollection instance. 

    • Edited by Petr B Sunday, May 13, 2018 6:39 PM
    Sunday, May 13, 2018 6:21 PM
  • Hi Victor Nikolov,

    Because ToString is not a static method, you could use it with class name, we need to use it via Freight instance, and the following code for your reference.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp6
    {
        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(string.Join(", ", freights.Select(t => t.ToString())));
                }
                return sb.ToString();
            }
            public IReadOnlyCollection<Freight> Freights
            {
                get => freights.AsReadOnly();
            }
    
            public void AddFreight(Freight freight)
            {
    
            }
    
        }
    
        class Freight
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return Name;
            }
        }
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 2:44 AM
    Moderator