Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a lambda expression has resulted in a strange error when attempting to compile

Tags:

c#

lambda

So currently there is a piece of code which looks like this...

string name = GetValues(sequenceOfCodes, 0, IDtoMatch, 1)[0];

I just updated the following line to be

string name = sequenceOfCodes
                  .Select(x => x[0])
                      .Where(x => x == IDtoMatch)
                          .FirstOrDefault();

Which should hopefully return the same thing.

sequenceOfCodes is a List<List<String>> and the IDtoMatch is also a string.

So hopefully this all seems fine.

However when I go to compile I get an odd error

The type 'System.Windows.Forms.ComboBox' is defined in an assembly 
that is not referenced. 

You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089'

And when I take my newly added code away it compiles fine and runs... So why is it just because I have added a lambda expression does it think that I it needs a reference to System.Windows.Forms.ComboBox?

Just to state that this is a console application. Not a winforms application.

-----------UPDATE----------

Ok, So I have found that a one of the references does reference the System.Windows.Forms, which I is really disappointing as this is core code and should not have dependencies like this :(

However I still wonder why the error did not appear before until after I added my line of code.

To confirm, If I remove my code I can close VS down and restart and rebuild and all is fine. If I add my line of code and close down and restart, etc. The error will reappear on rebuild.

Very strange error to me.

Thanks guys for all your help

like image 305
Secret Squirrel Avatar asked Oct 23 '13 08:10

Secret Squirrel


People also ask

What happens when Lambda throws exception?

A lambda expression cannot throw any checked exception until its corresponding functional interface declares a throws clause. An exception thrown by any lambda expression can be of the same type or sub-type of the exception declared in the throws clause of its functional interface.

What is the type of a lambda expression in Java 8?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

When to use Lambda expressions in Java?

Hence lambda expressions enable us to write functional code. Readable and concise code: People have started using lambda expressions and reported that it can help to remove a huge number of lines from their code.


1 Answers

You mention that one of the other projects does reference windows forms. My guess is that this project also declares some extension methods that are in scope (given your using directives), and which the compiler needs to explore for overload resolution - presumably of the Where, Select or FirstOrDefault methods; meaning: it can't decide that the best overload of these is the System.Linq.Enumerable one until it has compared it to the other candidates, and it can't do that without being able to understand the types used in the competing method signatures.

Or in other words: is there a Select, Where or FirstOrDefault custom extension method that mentions ComboBox ?

like image 169
Marc Gravell Avatar answered Sep 30 '22 11:09

Marc Gravell