0

I did create an enum in order to choose between different options. In the last stage I get the choosen enum back.

But something is wrong with my switch case expression.

Enum:

  public enum class MyObjForm { Rechteck, Ellipse };

Die Klasse:

  public ref class Fenster2: public System::Windows::Form{
        private: MyObjForm ^ form;
                 double breite;
                 double hoehe;

        Fenster2(MyObjForm ^ obj, double h, double b) : form{ obj }, hoehe { h }, breite{ b }
    {
        InitializeComponent();
    }
 ..
 }

Switch Case:

  switch (form) {
    case MyObjForm::Rechteck:
        gr->DrawRectangle(pen, 30.0f, 30.0f, breite, hoehe);
        break;
      case MyObjForm::Ellipse:
        gr->DrawEllipse(pen, 30.0f, 30.0f, breite, hoehe);
        break;
}

Visual Studio says there is a mistake in switch(form) it should be an integral type or an enumeration type.

But than it says: switch expression of the type "MyObjForm ^" isn't permitted.

And that the constant expressions aren't correct.

I do not see the correct answer, but I do not like to change from enum class to enum. Class is there to protect it, this way I think it should be there, even if it would be easier without. I use enum in order to learn how to use it.

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • 1
    what is that character `^` in the type specification ? – bruno May 02 '19 at 15:38
  • 2
    Doesn't `^` denote a managed reference? I think `MyObjForm ^ form` should rather be `MyObjForm form` and `MyObjForm` a native enum. – Timo May 02 '19 at 15:38
  • In CLI ^ denote a managed referenz, in C++ it would be *. Since I could write enum class I did try to use this way against only enum. – Chiru Kaio May 03 '19 at 05:49

2 Answers2

1
public enum class MyObjForm { Rechteck, Ellipse };

MyObjForm ^ form;

Managed enums are value types, not reference types. The ^ indicates a managed reference. A reference to a value type is unnecessarily boxing it, which is a weird type in C++/CLI, and isn't even possible in C#.

Remove the ^ from all places you use MyObjForm, and you'll be fine.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • Could you discribe when to use enum class and when enum? Since it is possible to use enum class there must be reasons why to use it and how to use it. I know that with enum I could use int 0, 1 etc together with the enum value like in my case Rechteck / Ellipse . Where I can only call the Referenz with enum class by using it together with the namespace, e.g. MyObjForm::Rechteck. At least this is what I do understand. – Chiru Kaio May 03 '19 at 05:56
  • Use `public enum class` when writing managed code, which you are here. If you're writing unmanaged code, use either `enum` for the old-style enum, or C++11's `enum class` (without `public`). See [this question](https://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum) for why `enum class` and `public enum class` are preferred. – David Yaw May 03 '19 at 13:59
0

I made it!

switch((MyObjForm)form){
//cases
}

I did need (MyObjForm)form in order to use enum class and it works