-4
namespace TaskBarPlus {
public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();
    }

    public class General {
        public enum ProcessPriority { Low = 0, Normal = 1, High = 2 }; ProcessPriority _Priority;

        [Category("Settings"), DisplayName("Application Priority")] public ProcessPriority Priority {
            get { return _Priority; }
            set {
                _Priority = value;
                switch (value) {
                    case 0: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
                    case 1: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
                    case 2: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                }
            }
        }
    }


    private void Form2_Load(object sender, EventArgs e) {
        General general = new General();
        propertyGrid1.SelectedObject = general;
    }
}

}

Error Cannot implicitly convert type 'int' to 'Form2.General.ProcessPriority'. An explicit conversion exists (are you missing a cast?)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Haithomy
  • 73
  • 2
  • 8

2 Answers2

2

Why not just use the actual enumeration values in the switch statement

public class General {
    public enum ProcessPriority { Low = 0, Normal = 1, High = 2 }; 

    ProcessPriority _Priority;

    [Category("Settings"), DisplayName("Application Priority")] 
    public ProcessPriority Priority {
        get { return _Priority; }
        set {
            _Priority = value;
            switch (value) {
                case ProcessPriority.Low: 
                    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
                    break;
                case ProcessPriority.Normal: 
                    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
                    break;
                case ProcessPriority.High: 
                    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                    break;
            }
        }
    }
}

The exception message states that it is not valid to implicitly cast an integer to ProcessPriority. It kind of makes sense, since ProcessPriority priority = -423 wouldn't make much sense in the context of ProcessPriority. Since you explicitly knew the values, you could have alternatively cast the integer like so

case (PriorityClass) 1:

or alternatively

int priorityVal = (int) value;
switch (priorityVal) {
    case 0:
        ....
}
Jaanus Varus
  • 3,508
  • 3
  • 31
  • 49
  • Error: Control cannot fall through from one case label ('case 0:') to another – Haithomy Jun 06 '15 at 20:56
  • great its work but how to use index with enum and call it in switch as case 0, case 1, ... ? – Haithomy Jun 06 '15 at 21:01
  • this work: public enum ProcessPriority:int { Low = 0, Normal = 1, High = 2 }; ProcessPriority _Priority; now I can switch with index – Haithomy Jun 06 '15 at 21:10
0

That's because your property Priority is of type ProcessPriority as can be seen below

public ProcessPriority Priority {

So the value is ProcessPriority and in your Switch you are saying case 0 where 0 is of Int32 type.

You should rather say like

switch(value)
{
  case ProcessPriority.Idle:
  ....
}
Rahul
  • 76,197
  • 13
  • 71
  • 125