tags:

views:

236

answers:

4
Dictionary<string, int> myList = new Dictionary<string, int>();
List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>(myList);
result.Sort((first, second) => second.Value.CompareTo(first.Value));

it throws 5 errors on line 3 while building

here's the screenie shottie from ASP.NET 2.0 alt text

this is from the Console app in .NET 2.0 alt text

so what do you think went wrong?

for John alt text

ok.. so here's a RAR file which contains a Console app and a Web app written for .NET 2.0 Lambda.rar

A: 

Have you ensured you're putting this code inside a method? Or do you have it at the class level. Having it at the class level throws 5 errors on build.

Bad

class Program
{
    var myList = new Dictionary<string, int>();
    var result = new List<KeyValuePair<string, int>>(myList);
    result.Sort((x, y) => x.Value.CompareTo(y.Value));  

    static void Main(string[] args)
    {

    }
}

Gives the following errors:

Error 1 Invalid token '(' in class, struct, or interface member declaration
Error 2 Invalid token ',' in class, struct, or interface member declaration
Error 3 Invalid token ')' in class, struct, or interface member declaration
Error 4 Invalid token '(' in class, struct, or interface member declaration
Error 5 Invalid token ')' in class, struct, or interface member declaration

Good

class Program
{
    static void Main(string[] args)
    {
        var myList = new Dictionary<string, int>();
        var result = new List<KeyValuePair<string, int>>(myList);
        result.Sort((x, y) => x.Value.CompareTo(y.Value));  

    }
}
David Morton
Hi David Morton :D Yes of course :) am doing this lambda in ASP.NET. Am not getting any problem when trying to write this code in Console or Windows Forms :) this "result.Sort((x, y) => x.Value.CompareTo(y.Value));" line while "Building" the solution returns 5 errors which was ") Expected", "Invalid expression term '>'", "; expected", "; expected", "Invalid expression term ')'"
Nullstr1ng
it would be nice if you can try that lambda in ASP.NET
Nullstr1ng
@Null - are you on C# 2, by any chance?
Kobi
might want to take a look at the screen shot
Nullstr1ng
@kobi, yes. am using 2.0, but lambda is working in Console or Windows Forms.
Nullstr1ng
Lambda is only recognised in Visual Studio 2008 and above, not 2005.
ck
@ck good to know. I am using VS 2008 :)
Nullstr1ng
@Nullstr1ng: what made you think it was working in Console or Windows Forms? It wasn't working. Unless Console and Windows forms were C# 3.0, and ASP.NET is C# 2.0.
John Saunders
@JohnSaunders. ok let's make another screen shot.
Nullstr1ng
http://img705.imageshack.us/img705/3618/lambda3.jpg
Nullstr1ng
@Nullstr1ng: That latest screenshot shows that you're targeting .NET 2.0...
Seth Petry-Johnson
@Nullstr1ng: do you think we're making a joke here? Lambda expressions were not introduced until C#3.0. That's a simple fact. It has nothing to do with ASP.NET vs. Console applications. Somehow, your ASP.NET site is using C#2.0, and your Console application is using C#3.0.
John Saunders
you can download Lambda.rar
Nullstr1ng
am not making a Joke. I am asking for help
Nullstr1ng
+2  A: 

Your second screenshot crops off the top of the file (the Using directives), and you explicitly mention ".NET 2" whether by accident or design. So, the obvious question:

Are you using C# 3? Because Lamdas weren't available in C#2. (Note: it's the language version and not the /NET framework version that matters!)

Multi-edit: Damn, my first edit made me even more incorrect :-)

Dan Puzey
Thanks. Am not using Linq. Just 2.0
Nullstr1ng
Edited to correct - I was thinking lamdas and wrote about linq instead. Linq is part of .NET 3.5; lamdas are part of C# 3.
Dan Puzey
A: 
using System;
using System.Collections.Generic;

namespace SO
{
    class Program
    {
        public static void Main(string[] args)
        {
            var myList = new Dictionary<string, int>();
            myList.Add("a",2);
            myList.Add("b",50);
            myList.Add("c",6);

            var result = new List<KeyValuePair<string, int>>(myList);
            result.Sort((first, second) => second.Value.CompareTo(first.Value));
            result.Reverse();

            foreach (KeyValuePair<string, int> pair in result)
            {
                Console.WriteLine(pair.Value);
            }

            Console.ReadLine();
        }
    }
}

The above seems to compile under 3.5 and 2...

Works too - just ran it.

Martin Milan
Thanks. That's working properly .. It's just weird that it doesn't work in ASP.NET 2.0
Nullstr1ng
@Martin: it has nothing to do with the framework version. It's a question of the compiler being used. You're clearly using C#3.0 with either Framework 2.0 or 3.5. It will not work with C#2.0.
John Saunders
you can download Lambda.rar
Nullstr1ng
+3  A: 

Just to try to get this in one place:

The C# programming language is largely independent of the .NET Framework. One example of this is that Visual Studio 2008 introduced version 3 of the C# programming language, which supported lambda expressions. That same version of Visual Studio 2008 also introduced version 3.5 of the .NET Framework. It also introduced the ability to target either version 2.0, 3.0 or 3.5 of the Framework, while allowing you to use version 2.0 or 3.0 of the language.

This allows you, for instance, to use C# 3.0 features in a program that targets version 2.0 or 3.0 of the .NET Framework.

Somehow, your ASP.NET application (or is it a web site) is set to use version 2.0 of the C# programming language. Your Console application is set to use version 3.0. That is why it works in your console application and not in your ASP.NET application.

ASP.NET has always, and will always, support the same .NET Framework and C# programming language features as a Console application. If you're seeing a difference between the two, then it's a difference in your settings, not a difference in the platforms. This is based on my knowledge of ASP.NET since the betas of version 1.0.

John Saunders
Thanks for clarification John. I appreciate it. I was checking the .csproj from Lamda Console app. I saw this <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> does it still use 3.0 even if the project was set to .NET 2.0?
Nullstr1ng
anyway, I uploaded a sample code here http://uploading.com/files/a1734ceb/Lambda.rar/
Nullstr1ng
@Nullstr1ng: the framework has nothing to do with it. Only the compiler version does.
John Saunders
ok. Thanks for that information. +1
Nullstr1ng