Khaled
Khaled

Reputation: 3

Im getting an error everytime i try to add a value to my list

I have this question where we have to make a weather app type thing? basically the user can add a city and a temperature. The user can also view the cites that have been added and remove one if they wish to. I got stuck on adding a string to my list which is in different class, don't understand what am doing wrong.

    public class Program {
    public static void Main(string[] args) {
        double size;
        int menu;
        bool running = true;
        while (running) {
            Console.WriteLine("1. Add a city");
            Console.WriteLine("2. View cities");
            Console.WriteLine("3. Remove a city");
            Console.WriteLine("4. Quit");
            string str = Console.ReadLine();
            int.TryParse(str, out menu);

            var city = new stad();

            switch (menu) {
                case 1:

                    Console.Write("The amount of cities you want to add: "); //Anga temperatur som ska anges
                    size = double.Parse(Console.ReadLine());

                    //for loop för att ange värderna
                    for (int i = 0; i < size; i++) {
                        Console.Write($"City {(i+1)} : ");
                        string stadString = Console.ReadLine();
                        city.City.Add(stadString);

                        Console.Write($"Temperature for city {(i+1)} : ");
                        double stadDouble = double.Parse(Console.ReadLine());
                        city.Temperature.Add(stadDouble);
                    }
                    Console.Clear();

                    break;

                case 2:

                    break;

                case 3:

                    break;

                case 4:
                    running = false;
                    break;
            }
        }
        Console.ReadLine();
    }
}

class stad {
    public List<string> City { get; set; }
    public List<double> Temperature { get; set; }

}

Upvotes: 0

Views: 60

Answers (1)

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

You must instantiate the List<> class before you can use it. You are constructing the stad class, but the City and Temperature properties aren't created anywhere.

Try this:

class stad {
    public List<string> City { get; } = new List<string>();
    public List<double> Temperature { get;  } = new List<double>();

}

It will make sure that if you create an instance of stad, it will create the City and Temperature as well.


Update:

I would go for a City class which contains the city name and temperature. This way you keep related information together.

For example:

public class CityWithTemperature
{
    public string CityName {get; set; }
    public double Temperature {get; set; }
}

public static void Main(string[] args) 
{
    // declare a list which could contain CityWithTemperature classes
    var cityTemperatures = new List<CityWithTemperature>();

    // Create the first city
    var firstCity = new CityWithTemperature();
    firstCity.CityName = "New York";
    firstCity.Temperature = 18.4;
    cityTemperatures.Add(firstCity);

    // create the second city
    // you could also write it shorter:
    cityTemperatures.Add( new CityWithTemperature
    {
        CityName = "Houston",
        Temperature = 10.4
    });

    // display them
    foreach(var cityInfo in cityTemperatures)
    {
        Console.WriteLine($"{cityInfo.CityName} with a temperature of {cityInfo.Temperature}");
    }

    // or a for loop:
    for(int i=0;i<cityTemperatures.Count;i++)
    {
        Console.WriteLine($"{cityTemperatures[i].CityName} with a temperature of {cityTemperatures[i].Temperature}");
    }

}

Upvotes: 1

Related Questions