473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.N ame));
}

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 4468
On Sat, 26 Jul 2008 10:39:17 -0700, K Viltersten <tm**@vilterste n.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.N ame));
}

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**@vilterste n.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((nu mb) =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**@vilterste n.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.Na me)" becomes "delegate (string f) { return
re.IsMatch(f.Na me); }"), 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**@vilterste n.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((nu mb) =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.co m>
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...@vilterste n.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
3627
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
63
3343
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things suddenly become harder than they need to be. An example of what I mean is a quick script I wrote for doing certain actions based on a regexp, which I...
6
1601
by: Xavier Décoret | last post by:
Hi, I cannot find the way to do generic lambda functions using the lambda syntax in python. I am probably missing a point. For example, the code # f = lambda : print "hello" # f()
181
8709
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
5
2150
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a bit complicated so any explanation would be appreciated. Thanks
15
4826
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a =a++; F fAnonymous = delegate(int a) { return a++; }; fLambda(1);
2
2192
by: =?Utf-8?B?UnV0aCBNYXJr?= | last post by:
Hey, Am trying to write a lambda expression for a delegate, am getting an error as ------------------------------ only assignment, call, increment, decrement, and new expression can be used as a statement ----------------------------- For the following code.
21
1832
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the following function? def addn(n): return lambda x,inc=n: x+inc if i do addn(5) it returns
11
1673
by: puzzlecracker | last post by:
I am not understanding what's happening in the code below (from accelerated c#) -- I know what the outcome but not how .net does it: using System; using System.Linq; public class LambdaTest { static void Main()
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.