Problems compiling a lambda expression

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I've got a hint from a gentleman here to use the
following syntax.

public static FileInfo[]
GetFilesRegExp(this DirectoryInfo di, string pat)
{
Regex re = new Regex(pat);
return Array.FindAll(
di.GetFiles(),
(f) => re.IsMatch(f.Name));
}

The problem is that the compiler seems to dislike
"this" in the argument list as well as "goes to"
in the lambda expression.

What's up with that and how do i kill it?
 
The problem is that the compiler seems to dislike
What errors are you getting _exactly_. Writing "the compiler seems to
dislike" doesn't explain your problem at all.

As far as the "this", I expect that either you're not using C# 3.0 or
you're trying to declare the method in a non-static class. But without
knowing the error, it's hard to say for sure which or whether either is
correct.

I don't see anything obviously wrong with the lambda expression, but
again...if you're not using C# 3.0, then it wouldn't work.

Knowing the actual error message would help a _lot_. Frankly, that's
always going to be true if you are asking about help with errors that
occur, either during compilation or execution.

I could argue that i provided just enough information, my
helpful friend. In fact, i was using v2.0, which i wasn't
aware of until you pointed it out. Thanks a million!

(Of course, i agree with you - one should almost always
provide the error messages. In this case, i KNEW it
couldn't be that. However, for future reference, i'll do it.)

Now, i was sure that installing VS 2008 upgraded my
framework to v3.5. Obviously, it didn't... Is the upgrade
to the latest version of the framework free of charge if
i'm already on a paid version of VS2005?
 
Now, i was sure that installing VS 2008 upgraded my
If you're using VS 2008, you're using C# 3.0. In VS 2008, projects can
target any specific version of .NET from 2.0 on. The default ought to be
.NET 3.5, but the issues you're asking about are language/compiler issues
so even if you accidently targeted an earlier version of .NET, using VS
2008 should still allow it to compile. Conversely, just having the .NET
3.5 SDK installed isn't sufficient. You need to be using the compiler
that supports C# 3.0 features, which is VS 2008.

In other words, we're back to my previous comment: without knowing the
exact error message, it's impossible to say for sure what the problem is.

First of all - thanks for help. It's needed and apreciated!

Secondly, this is the stuation on my computer today.
VS2005, v8.0
DotNet framework v2.0
MS VC# 2005
According to the lis tin the control panel, i have a bunch of frameworks
installed: v1.1, v.2.0, v3.5 and so on.

The error message i perhaps should have provided is as follows.
Error 3 Invalid expression term '>'
C:\dev\dummies\XMLReading01.cs 36 24 DummySpace

The two lines causing the problems are as follows.
int[] arr = new int[] { 1, 13, 5, 3, 12, 1, -3, 4 };
arr.Average((numb) => numb > 4);
 
K Viltersten said:
First of all - thanks for help. It's needed and apreciated!

Secondly, this is the stuation on my computer today.
VS2005, v8.0
DotNet framework v2.0
MS VC# 2005
According to the lis tin the control panel, i have a bunch of frameworks
installed: v1.1, v.2.0, v3.5 and so on.

The error message i perhaps should have provided is as follows.
Error 3 Invalid expression term '>'
C:\dev\dummies\XMLReading01.cs 36 24 DummySpace

The two lines causing the problems are as follows.
int[] arr = new int[] { 1, 13, 5, 3, 12, 1, -3, 4 };
arr.Average((numb) => numb > 4);

And what are you building with? The command line? Visual Studio? If
you're building with the command line, you need to make sure you're
using one with the correct environment variables set up.
 
Secondly, this is the stuation on my computer today.
This is your problem.

Ah, that explains SO much. Thanks.
You can either revert to the older "delegate" syntax (so "(f) =>
re.IsMatch(f.Name)" becomes "delegate (string f) { return
re.IsMatch(f.Name); }"), or you can switch to the newer compiler (the most
obvious way would be to just use the 2008 version of VS, either Visual C#
Express 2008 or the retail version of Visual Studio 2008).

I'm forbidden from upgrading (boss's order) until november. Can i
upgrade the compiler itself so it WILL compile the cooler (read:
from v3+) stuff as well? I hope purchasing VS2008 nor getting
VS2008 Express isn't a requirement... I am not allowed on the
sofa, you see (sofa = VS2008 any version)...
 
I'm forbidden from upgrading (boss's order) until november. Can i
I'm sorry, but I don't know the answer to that question. For one, I've
never tried to upgrade the compiler for the IDE without upgrading the IDE
itself. That might work, but it also might not. For another, if your
boss is prohibiting you from upgrading the IDE, that prohibition may well
extend to the compiler as well, even if you only use the compiler from the
command line rather than in the IDE.

I intend to growl disappointedly until november...
Thanks for your time!
 
K Viltersten said:
I'm forbidden from upgrading (boss's order) until november. Can i
upgrade the compiler itself so it WILL compile the cooler (read:
from v3+) stuff as well? I hope purchasing VS2008 nor getting
VS2008 Express isn't a requirement... I am not allowed on the
sofa, you see (sofa = VS2008 any version)...

If you've got .NET 3.5 installed, you've got the C# 3.0 compiler
installed - but you can't make VS 2005 use it. So you could build C# 3
code from the command line, but not from VS 2005.
 
If you've got .NET 3.5 installed, you've got the C# 3.0 compiler
installed - but you can't make VS 2005 use it. So you could build C# 3
code from the command line, but not from VS 2005.

I'll consider it. It'll be like in the good old
days, when i could type:
javac Main.java -path...
and what not by heart. Really fast too...

Thanks!
 
K said:
I'll consider it. It'll be like in the good old
days, when i could type:
javac Main.java -path...
and what not by heart. Really fast too...

Besides csc you can use msbuild or nant or ...

Arne
 
I'll consider it. It'll be like in the good old
days, when i could type:
  javac Main.java -path...
and what not by heart. Really fast too...

Another reason not to stick to fancy syntactic sugar stuff like
"lambda expressions" and to do things the old fashioned (C#2.0) way.
I just reviewed lambda expressions this morning, as well as Action and
Func, and it seems an enormous waste of time, designed to impress
fellow programmers, like the ternary conditional operator ?:, rather
than to get the job done.

RL
 
Another reason not to stick to fancy syntactic
sugar stuff like "lambda expressions" and to do
things the old fashioned (C#2.0) way.
I just reviewed lambda expressions this morning,
as well as Action and Func, and it seems an
enormous waste of time, designed to impress
fellow programmers, like the ternary conditional
operator ?:, rather than to get the job done.

Oh, c'mon a bit. Sometimes it's nice to play with
your coworkers and typing some really complicated
code they haven't seen before, nodding the head
and asking, "what do you think, guys?". It's a
great way to learn who's got the cojones to admit
they don't know what's going on. :)

I warmly recommend the language of APL , if one
wishes to impress/scare/astonish colleagues...
 
raylopez99 said:
Another reason not to stick to fancy syntactic sugar stuff like
"lambda expressions" and to do things the old fashioned (C#2.0) way.
I just reviewed lambda expressions this morning, as well as Action and
Func, and it seems an enormous waste of time, designed to impress
fellow programmers, like the ternary conditional operator ?:, rather
than to get the job done.

That suggests you haven't really understood how useful they can be - in
particular, LINQ relies on them.

Lambda expressions are very, very powerful. I just wish Java had them
:(
 
Don't worry about Ray.  He dismisses as useless pretty much everything in  
C# that he doesn't fully understand.  I don't think that will ever change.


That's not true. I dismissed as useless delegates until such time
that I started using them.

RL
 
That suggests you haven't really understood how useful they can be - in
particular, LINQ relies on them.

Lambda expressions are very, very powerful. I just wish Java had them
:(

LINQ is just a poor man's SQL. Be a man and learn the real thing.
That said, when I start learning LINQ I might change my mind...

RL
 
LINQ is just a poor man's SQL. Be a man and learn the real thing.
That said, when I start learning LINQ I might change my mind...

How about learning it *before* dismissing it as being poor man's SQL? LINQ
is by far the best approach I've seen to integrate relational query syntax
into a "normal" language. Integration has many advantages that are otherwise
very hard to realize, e.g. compile-time type-safety.

Regards,
 
Back
Top