Reputation: 2331
I made enum and tried to go through it with for-each loop and look for different cases using switch case and get values of dives used. ERROR I AM GETTING IS cannot implicitly convert type'console5.typeOfDevice' to 'string'
enum typeOfDevice
{
iphone=99,
android=59,
tablet=49
}
var usedDevice = Enum.GetNames(typeof(typeOfDevice));
foreach (string used in usedDevice)
{
switch (used)
{
case usedDevice.iphone: //gets error here
Console.Write($"enter how many time you used Iphone");
var input = Console.ReadLine();
break;
case usedDevice.android:
Console.Write($"enter how many time you used Android");
var input = Console.ReadLine();
break;
case usedDevice.tablet:
Console.Write($"enter how many time you used Tablet");
var input = Console.ReadLine();
break;
default:
break;
}
}
Upvotes: 0
Views: 249
Reputation: 461
Enum.GetNames
returns a string array. In your loop, you are trying to get usedDevice.iphone
(etc) but usedDevice
is a string array, therefore you'll get an error. However, if you change usedDevice
to typeOfDevice
, you'll get the comparison error you've edited into your question.
Namely, used
is of type string, and it cannot implicitly be converted to typeOfDevice
, since typeOfDevice
is an enum, and not a string.
This question may be useful to you. In short, use GetValues
over GetNames
Upvotes: 1