?
Solved

visual studio lamba expression

Posted on 2014-08-23
17
Medium Priority
?
355 Views
Last Modified: 2014-08-24
The below code works but also causes a warning of: "Lambda expression will not be removed from this event handler. Assign the lambda expression to a variable and use the variable to add and remove the event."    

What change is necessary to resolve this warning?

            Dim cb2 As TextBox = TryCast(e.Control, TextBox)
            If cb2 IsNot Nothing Then
                Dim txtEdit As TextBox = DirectCast(e.Control, TextBox)
                RemoveHandler txtEdit.KeyPress, New EventHandler(AddressOf txtEdit_Keypress)
                AddHandler txtEdit.KeyPress, New EventHandler(AddressOf txtEdit_Keypress)
            End If
0
Comment
Question by:dastaub
  • 8
  • 6
  • 3
17 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40281135
Which line causes the warning, because you don't have any lambda expressions in what you posted above?
0
 

Author Comment

by:dastaub
ID: 40281287
when I mouse click on the warning message, it takes me to and highlights the below code:  

New EventHandler(AddressOf txtEdit_Keypress)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40281519
Can you post this event txtEdit_Keypress because the code you have posted as kaufmed stated does not have a Lambda expression in it.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:dastaub
ID: 40281590
Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        '
        If mName2GLOBAL = "Barcode" Or mName2GLOBAL = "LocationValue" Or mName2GLOBAL = "OwnerValue" Then
            e.Handled = True
            Exit Sub
        End If
        '
        If mName2GLOBAL = "Qty" And e.KeyChar = "." Then
            e.Handled = True
            Exit Sub
        End If
        '
        Dim Test As String = sender.text
        '
        If mName2GLOBAL.Contains("Row") = True And Len(Test) >= 12 Then   'fuck
            e.Handled = True
            Exit Sub
        End If
        '
        If mName2GLOBAL <> "Qty" And mName2GLOBAL <> "Price" Then
            Exit Sub
        End If
        '
        Dim mTest2 As Boolean = FindTwoDots(Test)
        '
        If mTest2 = False And e.KeyChar <> ChrW(Keys.Back) Then
            e.Handled = True
            Exit Sub
        End If
        '
        If (IsNumeric(e.KeyChar.ToString()) Or e.KeyChar = ChrW(Keys.Back) Or (e.KeyChar = "." And Test.Contains(".") = False)) Then   'If TxtRow4.Text.Contains(".") = True And e.KeyChar = ChrW(46) Then
            e.Handled = False
            Exit Sub
            e.Handled = False 'if numeric display
        Else
            e.Handled = True  'if non numeric don't display
        End If
    End Sub

    Private Function FindTwoDots(ByVal SearchIn As String) As Boolean
        FindTwoDots = True
        Dim word As String = SearchIn.Trim()
        Dim wordArr As String() = word.Split("."c)
        If wordArr.Length > 1 Then
            Dim afterDot As String = wordArr(1)
            If afterDot.Length >= 2 Then
                FindTwoDots = False
            End If
        End If
        Return FindTwoDots
    End Function
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40281614
Hi dastaub;

I do not see any Lambda expression in your code. Can you try and change your Add/RemoveHandler as shown below to see if the warnings go away.

RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress

Open in new window

0
 

Author Comment

by:dastaub
ID: 40281640
As you suggested, I changed the Add/RemoveHandler to see if the warnings went away.  The warnings did go away.  Does it make sense that visual studio used the term lambda expression to describe this issue?  The warnings are now gone.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40281661
I was going to mention earlier that this line:

RemoveHandler txtEdit.KeyPress, New EventHandler(AddressOf txtEdit_Keypress)
...probably wouldn't do what you wanted anyway. Basically when you add an event handler to a object, there is a collection that stores each handler that you attach--which means you can add multiple handlers. In order to remove handlers from that collection, you have to have a reference to the handler that you wish to remove. Because you passed in new EventHander(...), you don't have a reference--meaning you didn't store that instance to a variable before passing it to RemoveHandler. I suspect that .NET maybe creates a lambda under the hood to represent this? In any event, I would have expected to see:

Dim handler = New EventHandler(AddressOf txtEdit_Keypress)
AddHandler txtEdit.KeyPress, handler)

...  ' Later

RemoveHandler txtEdit.KeyPress, handler)
0
 

Author Comment

by:dastaub
ID: 40281811
now that the warning is gone, the issue is resolved?  

What would have been the consequence of not making the change to the code?  Would objects have accumulated in memory that would have been destroyed by the GC once scope was lost?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40281845
Hi dastaub;

To your question, "Does it make sense that visual studio used the term lambda expression to describe this issue?", by looking at the source code you would think not but when the source is compiled it could be using a Lambda to do the function. So I do agree with kaufmed.

Just in case you don't know what a Lambda expression looks like, it could take on of the following forms.

Function(x) y
Sub(x) y

Where x is an input object that y will perform some operations on. The difference between the two is that the Function will return a value and Sub does not return a value.
0
 

Author Comment

by:dastaub
ID: 40281866
Would objects have accumulated in memory that would have been destroyed by the GC once scope was lost?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40281867
Hi dastaub;

Microsoft documentation for the AddHandler takes the form, AddHandler event, AddressOf eventhandler, and the RemoveHandler takes the same form. The AddressOf eventhandler returns an instance of a delegate to the event handler and what you need in this case not to create a new instance to an event handler.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40281890
To your question, "What would have been the consequence of not making the change to the code?  Would objects have accumulated in memory that would have been destroyed by the GC once scope was lost?", I do not know the answer to that question. But if  I may give my reason why it would stick around until the program ends is that each time you executed that part of the code it would add a new delegate for that event and as the warning stated, "Lambda expression will not be removed from this event handler.", therefore it will remain around.
0
 

Author Comment

by:dastaub
ID: 40281896
and now it can be said a new delegate would not be created each time that code is executed?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40281908
Now the event will be Removed and Added each time through using the delegate returned from AddressOf which is what you want to happen.
0
 

Author Comment

by:dastaub
ID: 40281911
OK, Thank You.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40281912
Hmm, according to this page, both statements are equivalent:

http://msdn.microsoft.com/en-us/library/74wy9422(v=vs.90).aspx
0
 

Author Comment

by:dastaub
ID: 40281913
one creates a warning and the other does not.  i assume they are equivalent in the sense they both do work.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question