C
C#3y ago
DayKnight

✅ Convert Enum to string

How would I manage to convert an Enum into a string?
if (vichleType == vehicleEnum.Bil.ToString())
{
Mechanic Martin = new Mechanic("Martin", "Jensen", 11111111, vehicleEnum.Bil);
}
else if (vichleType == vehicleEnum.Motorcykel.ToString())
{
Mechanic Thomas = new Mechanic("Thomas", "Hansen", 22222222, vehicleEnum.Motorcykel);
}
else if (vichleType == vehicleEnum.Lastbil.ToString())
{
Mechanic Henrik = new Mechanic("Henrik", "Nielsen", 33333333, vehicleEnum.Lastbil);
}
if (vichleType == vehicleEnum.Bil.ToString())
{
Mechanic Martin = new Mechanic("Martin", "Jensen", 11111111, vehicleEnum.Bil);
}
else if (vichleType == vehicleEnum.Motorcykel.ToString())
{
Mechanic Thomas = new Mechanic("Thomas", "Hansen", 22222222, vehicleEnum.Motorcykel);
}
else if (vichleType == vehicleEnum.Lastbil.ToString())
{
Mechanic Henrik = new Mechanic("Henrik", "Nielsen", 33333333, vehicleEnum.Lastbil);
}
3 Replies
HimmDawg
HimmDawg3y ago
I think you can work with Enum.GetName. however, you should consider changing vehicleType to that enum type instead of string depends on your use case of course
DayKnight
DayKnightOP3y ago
Hmm... yeah Oo It's fixed I was trying to put information into my constructor in the wrong order
Accord
Accord3y ago
Closed!

Did you find this page helpful?