I have this enumeration for the days of the week. I have the variable "weekday" to convert it to the enum type. When I want to enter the day by keyboard, I get the error "Cannot implicitly convert the string type to NameDia.CS.Program.Days"
**namespace NombreDia_CS
{
class Program
{
enum Dias
{
Domingo = 1,
Lunes,
Martes,
Miercoles,
Jueves,
Viernes,
Sabado
}
static void Main(string[] args)
{
Dias diaSemana;
Console.WriteLine("Ingresar un valor numerico: ");
diaSemana = Console.ReadLine(); //the error here
}
}
}**
Console.ReadLine()
returns a string so you have to convert it into an integer and then cast it to the enum.
diaSemana = (Dias)Convert.ToInt32(Console.ReadLine());
Or you can do something like this in C# 7+ which will parse the string, and initialize diaSemana
with the corresponding Dias member
Enum.TryParse(Console.ReadLine(), out Dias diaSemana);
Which is also the same as this
Dias diaSemana;
Enum.TryParse(Console.ReadLine(), out diaSemana)