Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare lambda event handlers in VB.Net?

Tags:

I believe the following VB.Net code is the equivalent of the proceeding C# code; however the VB.Net test fails - the event handling Lambda is never called.

What is going on?

VB.Net version - fails:

<TestFixture()> _ Public Class TestClass     <Test()> _     Public Sub EventTest()         Dim eventClass As New EventClass         Dim eventRaised As Boolean = False         AddHandler eventClass.AnEvent, Function() (eventRaised = True)         eventClass.RaiseIt()         Assert.IsTrue(eventRaised)     End Sub     End Class  Public Class EventClass     Public Event AnEvent()     Public Sub RaiseIt()         RaiseEvent AnEvent()     End Sub End Class 

C# version - passes:

[TestFixture]     public class TestClass     {         [Test]         public void EventTest()         {             var eventClass = new EventClass();             var eventRaised = false;             eventClass.AnEvent += () => { eventRaised = true; };              eventClass.RaiseIt();             Assert.IsTrue(eventRaised);         }     }      public class EventClass     {         public delegate void EventHandler();         public event EventHandler AnEvent;         public void RaiseIt()         {             AnEvent();         }     } 
like image 601
Gareth D Avatar asked Oct 15 '08 13:10

Gareth D


People also ask

What is lambda expression in VB net?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression.

What is Event Handler Lambda?

The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.


2 Answers

For those finding this question now: since Visual Basic 2010 (VB 10.0), anonymous Subs do work, so you can write something like:

Sub() eventRaised = True 
like image 131
svick Avatar answered Sep 19 '22 17:09

svick


Note: This relates to older versions of VB.net Prior to Visual Studio 2010 and VB.net 10

The difference is that in VB.Net a lambda expression must return a value i.e. they must be functions not subs. The lambda expression eventRaised = true is being interpreted as a boolean expression rather than an assignment i.e. is evaluating to false rather than setting to true.

Further details on MSDN.

I'm don't think the c# pattern for testing events used in the example can be done in VB.Net without introducing another function e.g.

<TestFixture()> _ Public Class Test     <Test()> _     Public Sub EventTest()         Dim eventClass As New EventClass         Dim eventRaised As Boolean = False         AddHandler eventClass.AnEvent, Function() (SetValueToTrue(eventRaised))         eventClass.RaiseIt()         Assert.IsTrue(eventRaised)     End Sub      Private Function SetValueToTrue(ByRef value As Boolean) As Boolean         value = True         Return True     End Function  End Class  Public Class EventClass     Public Event AnEvent()     Public Sub RaiseIt()         RaiseEvent AnEvent()     End Sub End Class 
like image 38
Gareth D Avatar answered Sep 18 '22 17:09

Gareth D