0

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:

  1. I can access it by it's name (AccessMode.Name)
  2. I can easily convert it to int and back (well, i think it suitable for anything)
  3. I can override it's ToString() method
  4. 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?

Титан
  • 41
  • 1
  • 9
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method – jazb Jan 20 '22 at 04:55
  • The [`DisplayMember`](https://learn.microsoft.com/dotnet/api/system.windows.forms.listcontrol.displaymember) and [`ValueMember`](https://learn.microsoft.com/dotnet/api/system.windows.forms.listcontrol.valuemember) properties of your `ComboBox` may be what you need here, although you wouldn't be able to directly use an `AccessMode` as a data source. See [How do I have an enum bound combobox with custom string formatting for enum values?](https://stackoverflow.com/q/796607/150605) – Lance U. Matthews Jan 20 '22 at 07:43

1 Answers1

0

You should write the method ToString() like this:

 public class AccessMatrix
{     
    public AccessMode accessMode { get; set; }
    public enum AccessMode
    {
        Delete,
        Insert,
        Read 
    }
    
    public string ToString(AccessMatrix.AccessMode am)
    {
        switch (am)
        {
            case AccessMatrix.AccessMode.Delete: return "Удалить";
            case AccessMatrix.AccessMode.Insert: return "Добавить";
            case AccessMatrix.AccessMode.Read: return "Прочитать";

            default: throw new ArgumentException($"there's no such access mode");
        }
    }
}

And when you want to use this:

   AccessMatrix f = new AccessMatrix();
   f.accessMode = AccessMatrix.AccessMode.Delete;
   MessageBox.Show(f.ToString(f.accessMode));

Result in my case:

enter image description here

Aviv Halevy
  • 161
  • 1
  • 10