-4

I wanted to create a class constructor that Telefono as a parameter a string to insert into a list

The compiler I reports the error: "cannot implicitly convert to void a string"

How can I fix?

 public static class Telefono
    {
        public double LevelBattery { get; set; }
        public List<String> NomeTelefono { get; set; }
        public bool TelefonoON { get; set; }

        public Telefono(string telefono)
        {
            telefono = NomeTelefono.Add(telefono); //ERROR!!!!
        }

        public void ON()
        {
            Random x = new Random();
            int Batt = x.Next(100);
            LevelBatty = Batt;

            if (Levelbattery > 10)
                TelefonoON = true;
            else
            {
                Console.WriteLine("Ricaricare subito il telefono {0}. Batteria inferiore al 10%", NomeTelefono);
                Console.ReadLine();
                TelefonoON = false;
            }

        }
grilletto
  • 21
  • 4
  • `NomeTelefono` is null. Initialize it before using.... – L.B Nov 11 '14 at 18:41
  • *That* error seems unrelated to your string insertion. Are you sure its complaining about that line? You will get a NRE runtime error though (you need to instantiate the list). – BradleyDotNET Nov 11 '14 at 18:41

2 Answers2

7

List<T>.Add doesn't return anything. I think you just want to initialize your list and add the item:

public Telefono(string telefono)
{
    NoneTelefono = new List<string>();
    NomeTelefono.Add(telefono);
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

you need to add

NomeTelefono = new List<string>();

in the constructor

Z .
  • 12,657
  • 1
  • 31
  • 56