I use a enum called AccessMode
and I build a function for it:
public static string ToString(this AccessMatrix.AccessMode am)
{
switch (am)
{
case AccessMatrix.AccessMode.Delete: return "Удалить";
case AccessMatrix.AccessMode.Insert: return "Добавить";
case AccessMatrix.AccessMode.Read: return "Прочитать";
case AccessMatrix.AccessMode.Update: return "Обновить";
case AccessMatrix.AccessMode.Grant: return "Передать права";
case AccessMatrix.AccessMode.Unset: return "Не выбрано";
default: throw new ArgumentException($"there's no such access mode");
}
}
But it somewhy still uses the default ToString()
method (and it's not possible to add override
to the function - it says there's no such function to override). I need to override exactly this method because I want to override the default ToString()
behavior with my enum, not for using this method for me – I add this class to Combobox (Windows forms) that use ToString()
for displaying element's name. And I think it's not possible to change ComboBox's text, without losing the value.
I also tried to use this method, but I can't use switch
with it, it says "a constant value expected".
And yes, I'm not limited to using only enum. But there's the most suitable type for my purpose. What can you advice me? If ToString()
overriding is not possible for enum, what can you advice me? I need:
- I can access it by it's name (AccessMode.Name)
- I can easily convert it to int and back (well, i think it suitable for anything)
- I can override it's ToString() method
- It's constant
For now, I'm using the enum, writing value by mine AccessMode.ToString function and taking it back using string.ToAccessMode, but it doesn't feels right. Is there's a better way?