0

I have created a class called AirlineCompany which attribute have a collection of flight information. To have flight information, I created a class called Flight. Then in the AirlineCompany I created a indexer with type list for Flight, I also create a setter for it. In the main I tried to set value for the list but keep receive exception when running the code.

This is my class Flight:

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

namespace Assignment3
{
    class Flight
    {
        private int id;
        public int Id
        {
            get { return id; }
            set {  this.id = value; }
        }

        private string origin;
        public string Origin
        {
            get { return origin; }
            set { this.origin = value; }
        }

        private string destination;
        public string Destination
        {
            get { return destination; }
            set { this.destination = value; }
        }

        private string date;
        public string Date
        {
            get { return date; }
            set { this.date = value; }
        }

        private double price;
        public double Price
        {
            get { return price; }
            set { this.price = value; }
        }

        public Flight(int id, string origin, string destination, string date, double price) {
            this.id = id;
            this.origin = origin;
            this.destination = destination;
            this.date = date;
            this.price = price;
        }

        public bool FindFlight(int searchId)
        {
            if(searchId == this.id)
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            string s = "Flight ID: " + id + "\nOrigin: " + origin + "\nDestination: " + destination +
                        "\nDate: " + date + "\nPrice: " + price + "$";
            return s;
        }
    }
}

This is my AirlineCompany class:

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

namespace Assignment3
{
    class AirlineCompany
    {
        public static readonly string airlineName;
        static AirlineCompany()
        {
            airlineName = "Vietjet airline";
        }
        public List<Flight> flights= new List<Flight>(100);
        public Flight this[int index]
        {
            set
            {
                flights.Insert(index, value);
            }
            get
            {
                return flights[index];
            }
        }
    }
}

This is my main class:

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

namespace Assignment3
{
    class Program
    {
        static void Main(string[] args)
        {
            AirlineCompany vn = new AirlineCompany();
            Console.WriteLine(AirlineCompany.airlineName);
            vn.flights[0] = new Flight(1,"Viet Nam","Finland","24/11",169.9);
            vn.flights[1] = new Flight(2, "Finland", "Singapore", "25/11", 109.9);
            vn.flights[2] = new Flight(3, "Singapore", "Finland", "26/11", 129.9);
            vn.flights[3] = new Flight(4, "Holland", "Germany", "27/11", 69.9);
            Console.ReadLine();
        }
    }
}

This is the exception when I run code:

This is the exception when I run code

halfer
  • 19,824
  • 17
  • 99
  • 186
CastAway
  • 7
  • 2
  • 1
    `new List(100);` will create an **empty** list with capacity of 100 elements. – Guru Stron Nov 16 '21 at 08:55
  • We have many kinds of collections and you should check base on situation. For example: - Array: You declare an array, you can set/get value by index but you can't change its length (like add, remove). It also means, index of an element in an array never changes. (Except when you move the value manually) - List: You are flexible to add/remove elements. It also means nothing can guarantee the index of an element. It can be changed unexpectedly by adding/removing elements. So, if you want to write a program and the data is determined by index, you may use an array instead of a list. – Võ Quang Hòa Nov 16 '21 at 09:11
  • Change ``` public List flights= new List(100); ``` by ``` public Flight[] flights= new Flight[100]; ``` Can help you resolve the issue – Võ Quang Hòa Nov 16 '21 at 09:11
  • 1
    @VõQuangHòa: please use single backticks for inline code formatting (either in comments or posts). I think, since you added spaces inside the backticks, they did not work in your comment above anyway. – halfer Dec 28 '21 at 14:17

1 Answers1

0

The int constructor argument of a List<T> e.g new List<Flight>(100) isn't its initial size, it's the Capacity.

Gets or sets the total number of elements the internal data structure can hold without resizing.

I.e. the pre-allocated internal backing array size.

Secondly, you are not using the Indexer you are using the member List property, you might as well just use the List for what it is

vn.flights.Add(new Flight(1,"Viet Nam","Finland","24/11",169.9));

If you wanted to use the Indexer, you would use the following syntax

vn[0] = new Flight(1,"Viet Nam","Finland","24/11",169.9);

However this is going to get you into all sorts of trouble fairly quickly, you would actually need to initialize the list with the appropriate size first (like you originally set out to do).

public List<Flight> flights= new List<Flight>(new Flight[100]);

In which case, why would you want this to be a list and not just an array?

public Flight[] flights = new Flight[100];

In short, an indexer seems like the wrong tool for the job here.

Though, if you really wanted to use it, it seems more normal just to proxy the call to the indexer of the list / array.

public Flight this[int index]
{
   set => flights[index] = value;
   get => flights[index];
}

Further reading

Indexers (C# Programming Guide)

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thank for the solution. I want to ask that the setter i create for the indexer is not working right? – CastAway Nov 16 '21 at 09:06