user11196706
user11196706

Reputation:

Accessing the 'Name' property of an object in C#

I am learning C# (I'm new to it) and am building a small Airport application. I want to be able to get the names of the planes that are in the airport rather than simply knowing that the List contains various Airport.Plane objects. My code at the moment is:

Airport.cs:

using System;
using System.Collections.Generic;

namespace Airport
{
    public class Airport
    {
        public List<object> planes;

        public Airport()
        {
            planes = new List<object>();
        }

        public List<object> Land(object plane)
        {
            planes.Add(plane);
            Console.WriteLine("Currently in the airport:");
            planes.ForEach(Console.WriteLine);
            return planes;
        }

        public List<object> TakeOff(object plane)
        {
            planes.Remove(plane);
            Console.WriteLine("Currently in the airport:");
            planes.ForEach(Console.WriteLine);
            return planes;
        }

        public int GetPlaneCount()
        {
            Console.WriteLine(planes.Count);
            return planes.Count;
        }
    }
}

Plane.cs:

using System;
namespace Airport
{
    public class Plane
    {
        public string Name { get; set; }

        public Plane(string name)
        {
            Name = name;
        }
    }
}



Program.cs:

using System;

namespace Airport
{
    class Program
    {
        static void Main(string[] args)
        {
            Airport airport = new Airport();
            Plane plane = new Plane("Private Jet");
            airport.Land(plane);
            airport.TakeOff(plane);
            airport.GetPlaneCount();
        }
    }
}


I tried to access the name of the Plane in the airport by Console.Log(plane.Name); but I get the following error: "'object' does not contain a definition for Name". Any help would be much appreciated!

Thanks

Upvotes: 0

Views: 185

Answers (2)

Seabizkit
Seabizkit

Reputation: 2415

some pseudo code to help you understand

public class Airport
{
    public string AirportName { get; set;}

    public List<Plane> planes;

    public Airport(string airportName)
    {
        AirportName = airportName;
        planes = new List<Plane>();
    }

    public List<Plane> Land(Plane plane)
    {
        planes.Add(plane);
        return planes;
    }

    public List<Plane> TakeOff(Plane plane)
    {
        planes.Remove(plane);
        return planes;
    }

    public int GetPlaneCount()
    {
        Console.WriteLine(planes.Count);
        return planes.Count;
    }
}

public void Main()
{
    var capeTownAirport = new Airport("Cape Town")

    var planeA = new Plane("facy plane");
    var planeB = new Plane("Another facy plane");

    var currentPlans = capeTownAirport.Land(planeA);            

    var currentPlans2 = capeTownAirport.Land(planeA);

    foreach(var plane in currentPlans2)
    {
         Console.WriteLine(plane.Name);
    }
}

Upvotes: 1

Stas Ivanov
Stas Ivanov

Reputation: 1245

Basically, C# is for the most part a statically typed programming language, so if you declare something as object you will only see object's properties, fields and methods. You can easily fix this by casting your object to Plane, but sticking to a known type is always the best way to go.

var plane = new Plane("My plane");

object myObject = plane;
// Console.WriteLine(myObject.Name); // Compiler complains here - object doesn't have Name property or field defined

Console.WriteLine((Plane)myObject); // Compiler is happy here, but you should never do it like that!

As it was said in the comments to your question, use strongly typed lists (List< Plane > instead of List< object >) and it will work fine.

Upvotes: 0

Related Questions