tags:

views:

44

answers:

2

Can anyone advise why this line of code would not compile? It generates CS1660 instead:

s.run_button.Invoke((b) => { b.Enabled = false; },
 new object[] { s.run_button });

Visual studio says: Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type

+1  A: 

Lambda expressions by themselves have no type and are not convertible to System.Delegate. The Invoke method has the type System.Delegate and hence it won't compile because the lambda expression has no type. You need to provide an explicit type conversion here

s.run_button.Invoke(
  (Action<Button>)((b) => { b.Enabled = false; }), 
  new object[] { s.run_button });
JaredPar
Thanks perfect.
Segfault
+1  A: 

The Invoke method takes a parameter of type Delegate. It was written before lambdas entered our world. The easiest solution for you is to wrap your lambda with an Action. I'm not sure precisely what type "b" is (and neither does the C# compiler, hence the error), so you'll have to pass it in explicitly. Something like:

s.run_button.Invoke(new Action<Button>(b => b.Enabled = false), new object[] { s.run_button });
Kirk Woll
Thanks, exactly what i needed.
Segfault