Anonymous methods

Sometimes I wish I was using C# instead of Visual Basic :-( And anonymous methods are one of the things that tend to bring that wish to the surface. Consider the following code:


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
   classProgram
   {
      staticvoid Main(string[] args)
      {
         List<DemoEntity> list = newList<DemoEntity>();
         list.Add(newDemoEntity(1, “one”));
         list.Add(newDemoEntity(2, “two”));
         list.Add(newDemoEntity(3, “three”));
         string theId = “two”;
         DemoEntity found;
         found = list.Find(delegate(DemoEntity obj) { return (obj.Id == theId); });
         Console.WriteLine(found.Id);
         found = list.Find(delegate(DemoEntity obj) { return (obj.Number == 3});
         Console.WriteLine(found.Id);
         found = list.Find(delegate(DemoEntity obj) { return (obj.Number == 1 && obj.Id == “one”); });
         Console.WriteLine(found.Id);
         Console.ReadKey();
      }
   }
 
   classDemoEntity
   {
      publicstring Id;
      publicint Number;
      public DemoEntity(int number, string id)
      {
      Number = number;
      Id = id;
      }
   }
}
 
 
Nice and elegant :-) To do the same in VB I would have to write three additional functions and store the variables to search for in fields, yuck :-(
 
Please, please, pretty please can we have anonymous methods in VB 9?
 
Maurice de Beijer


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>