BE 1.6 in VS 2005

Nov 20, 2010 at 3:52 PM
Edited Nov 20, 2010 at 6:03 PM

Hi Everybody,
In order to work with BE  source files in VS 2005  I had to changed few statements using  lambda to the old c# 2.0 syntax.
All went well for the 1.5 version which is working great, but as for version 1.6 of BE, I am still struggling with some issues, due to my rudimentary knowledge of C# 3.0 features.
The difficult line for me to translate to c# 2.0 syntax is:
"_referrersByDay.Add(refer.Day, new List<Referrer>() { refer });" in Referrer.cs, it is part of :

 private static void parseReferrers_ex()  
 {       
       _referrersByDay = new Dictionary<DateTime, List<Referrer>>();          
  
      foreach (Referrer refer in _Referrers)            
      {                
              if (_referrersByDay.ContainsKey(refer.Day))   {     _referrersByDay[refer.Day].Add(refer);     }                
              else  {  _referrersByDay.Add(refer.Day, new List<Referrer>() { refer });  }          
       }      
 }

 So far I have change the  line :
"else  {    _referrersByDay.Add(refer.Day, new List<Referrer>() { refer });  }  "
 to:
" else {  List<Referrer> refer_1 = new List<Referrer>();   _referrersByDay.Add(refer.Day, refer_1); } "

I have also tried: 
else
{  
         List<Referrer> refer_1 = new List<Referrer>();   
          refer_1 = refer;
           _referrersByDay.Add(refer.Day, refer); 

But it is giving the error:
"Cannot implicitly convert type 'BlogEngine.Core.Referrer' to 'System.Collections.Generic.List<BlogEngine.Core.Referrer>'"
and the biggest trouble is that I don't  know very well  how to do the casting in such <lambda> cases :)
Any help will be greatly appreciated.
Thank You in advance.

 

Coordinator
Nov 20, 2010 at 7:07 PM

BE 1.6 is using .NET 2.0.  And there are not any Lambdas in the code you posted here.  For example, this code:

_referrersByDay.Add(refer.Day, new List<Referrer>() { refer });  }

... is .NET 2.0 syntax, and not a lambda.  It's using a generic List<> which is part of .NET 2.0.

In the BE core project for 1.6, however, there is some use of lambdas.  Lambdas use the => syntax, btw.  For example the following is in the BE 1.6 core:

BlogRolls.Find(br => br.Id == id)

This gets compiled into code that will still run in a .NET 2.0 environment (.NET 3.0 is not needed).  VS 2005 probably won't recognize this lambda syntax, but it's only the BE core (a separate project from the website files).  So if you're only working with the Web files in VS 2005, then VS 2005 should be able to recognize the BE 1.6 code.  It's just the BE core that VS 2005 would have trouble with.  The Core is a separate C# class library project.

At any rate, the code you posted here is .NET 2.0 code, without any lambdas.

Nov 20, 2010 at 8:04 PM

Thank you for this swift reaction Ben.
You are right, no doubt, This lambda  terminology and  C# 3.0  stuff  is very new for me and I am very much confused about some things.
Anyway. I have found solution for the lambda parts like BlogRolls.Find(br => br.Id == id), using syntax which is working in my compiler eg. BlogRolls.Find(delegate(BlogRollItem br) {return br.Id == id; }); 
However the line : _referrersByDay.Add(refer.Day, new List<Referrer>() { refer });  will not compile in my .NET 2.0 environment ( even with .NET 3.5)
so I will have to find another way to get this working.
So far  I am using this syntax:  List<Referrer> refer_1 = new List<Referrer>();' creating new Referrer refer_1 and passing it to '_referrersByDay.Add(refer.Day, refer_1);'

 private static void parseReferrers()   
 {       
       _referrersByDay = new Dictionary<DateTime, List<Referrer>>();           
  
      foreach (Referrer refer in _Referrers)            
      {                
              if (_referrersByDay.ContainsKey(refer.Day))   {     _referrersByDay[refer.Day].Add(refer);     }                
              else
             {  
                  List<Referrer> refer_1 = new List<Referrer>();   
                   _referrersByDay.Add(refer.Day, refer_1); 
             }           

       }       
 }

and the BE is compiling well, and the blog website is working.

Still, I am nor sure if it is ok, that I am not passing the 'refer' variable from  'foreach (Referrer refer in _Referrers)     {}'
I apologize for my current lack of knowledge on this subject and I will be very grateful for any sugestion how to handle this.

 

Coordinator
Nov 20, 2010 at 8:58 PM

You're right ... this code:

_referrersByDay.Add(refer.Day, new List<Referrer>() { refer });

Even though it's not a lambda, it's using a collection initializer with I think was new in .NET 3.5.  It still compiles to .NET 2.0 code, but VS 2005 probably doesn't recognize it.  It's just a shortcut to populate the list without having to add the item separately (i.e. 1 line of code, instead of 2 or 3).  So it is equivalent to:

List<Referrer> referrers = new List<Referrer>();
referrers.Add(refer);
_referrersByDay.Add(refer.Day, referrers);
... and you're right too about using syntax like BlogRolls.Find(delegate(....)).  A lambda is a shortcut for that delegate syntax.

Nov 21, 2010 at 1:20 PM

Thank you very much for the lesson.
Got it all working in VS 2005 now.