-2

I have an enum of type uint in my class and a function with a uint argument. But when I call that function (setColor) with an enum as argument, I get the error:

Error   3   Argument 1: cannot convert from 'Test.Form1.colors' to 'uint'   

This is my class:

namespace Test{
    public partial class Form1 : Form
    {
        enum color : uint {off, red, yellow};


        setColor(uint color){
         ...
        }

        MyFunction()
        {
         setColor(color.red);
        }
    }
}

4 Answers4

2

You should read the documentation

The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is necessary to convert from enum type to an integral type. For example, the following statement assigns the enumerator Sun to a variable of the type int by using a cast to convert from enum to int.

int x = (int)Days.Sun;

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

Cast to uint and define the enum with {}.

public partial class Form1 : Form
{
    enum color : uint {off, red, yellow};

    void setColor(uint color){

    }

    void MyFunction()
    {
        setColor((uint)color.red);
    }
}
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
dave
  • 2,291
  • 3
  • 19
  • 25
  • I did define it with {}. Copy paste error. Why do i have to cast it to `uint` when I explicitly defined it as `uint`? – user3531722 May 06 '14 at 07:53
  • 1
    From MSDN http://msdn.microsoft.com/es-es/library/sbbt4032.aspx: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is necessary to convert from enum type to an integral type." – dave May 06 '14 at 08:04
1
enum mycolor : uint
{
    off, 
    red, 
    yellow
}

void setColor(mycolor color ){ }

void MyFunction()
{
    setColor(mycolor.red);
}
Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41
0

This should work for you.

public partial class Form1: Form
{
    enum color : uint {off, red, yellow};


    void setColor(uint color){

    }

    void MyFunction()
    {
        setColor((uint)(color.red));
    }
}
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281