0

I'm new in c# and can't find the solution.

I adding two car in an array.

I want to find the biggist car length in Greateslength().

Only I can't select Carlength or others at <--------- CAN'T SELECT

How can I select:

  • Carlength
  • Widthcar
  • CarInfo
  • CarBrand

in the Greateslength()method.

    abstract class Car
    {
        abstract int Getperimeter();
    }

    class Cars
    {
        public Car[] _Items = new Car[0];
        public int Count { get; set; }

        public void Add(Car car)
        {
            Count = Count + 1;
            Array.Resize(ref _Items, Count);
            _Items[Count - 1] = car;
        }

        public void Greateslength()
        {
            foreach (Car element in _Items)
            {
                int largestCarLength = 0;

                if (element. > largestCarLength) <--------- CAN'T SELECT Carlength 
                {
                    largestCarLength = element.; < ---------CAN'T SELECT Carlength 
                    Console.WriteLine(element.); < ---------CAN'T SELECT Carlength 
                                                 < ---------CAN'T SELECT Widthcar 
                                                 < ---------CAN'T SELECT CarInfo 
                                                 < ---------CAN'T SELECT CarBrand 
                }
            }
        }
    }

    class VW : Car
    {
        public VW(int carlength, int widthcar, string carInfo)
        {
            this.Carlength = carlength;
            this.Widthcar = widthcar;
            this.CarInfo = carInfo;
            this.CarBrand = "VW";
        }

        private double Carlength { get; set; }
        private double Widthcar { get; set; }
        private string CarInfo { get; set; }
        private string CarBrand { get; set; }

        public override double Getperimeter()
        {
            return Carlength * Widthcar;
        }


    static void Main(string[] args)
    {
        VW vw1 = new VW(10, 20, "car 1");
        VW vw2 = new VW(22, 11, "car 2");

        Cars c1 = new Cars();
        c1.Add(vw1);
        c1.Add(vw2);

        Console.WriteLine("--------------------");
        c1.Greateslength();
    }
lap_999
  • 1
  • 1
  • You can't access those properties because they don't exist in the base class. When you want to access the properties you have to move them from VW to the Car class – nikstffrs May 07 '20 at 20:23
  • Yes, thanks the must be public not private or protected.Containment doesn't work to. public double Carlength { get; private set; } – lap_999 May 07 '20 at 20:38

1 Answers1

1

You can't access the properties, because they are not defined in the base class Car which only consists of a single method Getperimeter().

If you want to access the members defined in VW then either cast your Car object to VW (that can only work, if the object is actually a VW) or you move your properties into the Car class.

Unfortunately that would not work either, because the properties you defined are set to private. You need to set them to public if you want to access them from an external class.

This would work in your case:

abstract class Car
{
    public double Carlength { get; set; }
    public double Widthcar { get; set; }
    public string CarInfo { get; set; }
    public string CarBrand { get; set; }

    abstract int Getperimeter();
}

Note that you don't need the defined properties in VW anymore. If you want to set them in the derived classes, but not from an external class, you may want to set the set accessor to protected.

Max Play
  • 3,717
  • 1
  • 22
  • 39