471,349 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Problems compiling a lambda expression

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?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 26 '08 #1
15 4347
On Sat, 26 Jul 2008 10:39:17 -0700, K Viltersten <tm**@viltersten.com>
wrote:
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?
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.

Pete
Jul 26 '08 #2
>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?

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?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 26 '08 #3
On Sat, 26 Jul 2008 11:08:15 -0700, K Viltersten <tm**@viltersten.com>
wrote:
[...]
>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.
Well, you can argue anything you like. That doesn't mean you'd be right.
:)
In fact, i was using v2.0, which i wasn't
aware of until you pointed it out. Thanks a million!
C# 2.0? Or .NET 2.0?
(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?
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.

_If_ my comments about language version have led you to an actual
solution, then _maybe_ you can try to argue that you've provided enough
information (but I'll just argue I'm a good guesser :) ). But if you
haven't solved the problem yet even with the answer I've given so far,
obviously you haven't provided enough information to answer the question
yet. :)

Pete
Jul 26 '08 #4
>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?

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);

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

Jul 26 '08 #5
On Sat, 26 Jul 2008 11:37:59 -0700, K Viltersten <tm**@viltersten.com>
wrote:
[...]
Secondly, this is the stuation on my computer today.
VS2005, v8.0
This is your problem.
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.
As I wrote, the .NET version doesn't help. You have to be using the right
compiler, and the 2005 version of the C# compiler isn't the right one. It
only supports C# 2.0.

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).

As far as the extension method you tried to declare, there is no C# 2.0
equivalent. If you can't or won't upgrade your compiler, you'll just have
to call the static method explicitly, and declare it without the "this" in
the parameter list.

Pete
Jul 26 '08 #6
K Viltersten <tm**@viltersten.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 26 '08 #7
<snip>
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!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 26 '08 #8
K Viltersten wrote:
<snip>
>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...
Besides csc you can use msbuild or nant or ...

Arne
Jul 27 '08 #9
On Jul 26, 3:02*pm, "K Viltersten" <t...@viltersten.comwrote:
<snip>
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...
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
Jul 27 '08 #10
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...

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 27 '08 #11
On Sun, 27 Jul 2008 08:39:36 -0700, K Viltersten <tm**@viltersten.com>
wrote:
[...]
>I just reviewed lambda expressions this morning,
as well as Action and Func, and it seems an
enormous waste of time, [...]

Oh, c'mon a bit.
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.

Pete
Jul 27 '08 #12
raylopez99 <ra********@yahoo.comwrote:
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
:(

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 27 '08 #13
On Jul 27, 12:37*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>
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
Jul 28 '08 #14
On Jul 27, 1:31*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
raylopez99 <raylope...@yahoo.comwrote:
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
:(
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
Jul 28 '08 #15
"raylopez99" <ra********@yahoo.comwrote in message
news:43**********************************@d45g2000 hsc.googlegroups.com...
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,

--
Andreas Huber

When replying by private email, please remove the words spam and trap
from the address shown in the header.

Jul 29 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

53 posts views Thread by Oliver Fromme | last post: by
63 posts views Thread by Stephen Thorne | last post: by
6 posts views Thread by Xavier Décoret | last post: by
5 posts views Thread by Octal | last post: by
2 posts views Thread by =?Utf-8?B?UnV0aCBNYXJr?= | last post: by
21 posts views Thread by globalrev | last post: by
11 posts views Thread by puzzlecracker | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.