Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:18:44+00:00 2026-06-17T22:18:44+00:00

I am using VB.Net 4 and am successfully using a class with the following

  • 0

I am using VB.Net 4 and am successfully using a class with the following signature:

Sub Register(Of TMessage)(recipient As Object, action As
System.Action(Of TMessage))

I want to learn about lambdas in VB so I wanted to see if I can simplify my current code.

CURRENTLY: I have the following in the constructor of a class (simplified for clarity)

Public Sub New()
    Dim myAction As New Action(Of String)(AddressOf HandleMessage)
    Messenger.Default.Register(Of [String])(Me, myAction)            
End Sub

And later in the class I have the following:

Private Function HandleMessage(sMsg As String)
    If sMsg = "String Value" Then
        If Me._Selection.HasChanges Or Me._Selection.HasErrors Then
            Return True
        End If
        Return False
    Else
        Return False
    End If
End Function

QUESTION: Is there a way to simplify this into a lambda like in C# where I don’t have to declare the MyAction variable in the constructor, but just pass the string value to HandleMessage function “inline” with the register sub? (I hope that makes sense)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:18:46+00:00Added an answer on June 17, 2026 at 10:18 pm

    So your constructor code is equivalent to:

    Messenger.[Default].Register(Of String)(Me,
        Function(sMsg)
            If sMsg = "String Value" Then
                If Me._Selection.HasChanges Or Me._Selection.HasErrors Then
                    Return True
                End If
                Return False
            Else
                Return False
            End If
        End Function)
    

    It’s worth mentioning though you don’t need to use lambdas just to get rid of your explicit delegate creation. This is perfectly legal too:

    Messenger.[Default].Register(Of String)(Me, AddressOf HandleMessage)
    

    There is a bit of strangeness though with your example: you’ve declared your Register method as taking an Action(Of TMessage) which means the function passed needs no return value. Your function however is returning a Boolean. Visual Basic here is doing the fancy “delegate relaxation” and is throwing away the return value. So all your Return True/Return False bits aren’t actually doing anything special.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone using .net actually worked out how to successfully sign a signature to
Is anyone successfully using Expression Blend Preview for .NET 4 with the RC of
Using .net 2.0 . I want to display a column in a DataGridView as
using .NET 3.5. I have a function I want to make multithreaded calls to.
I am creating the Entity Framework ObjectContext per ASP.NET Request using the following code:
I have the following setup: ASP.net 3.5 Web Site Project C# Class Library with
I'm building an ASP.NET MVC 2 application in C# and i am successfully using
I'm using the following classes for a one-to-many relationship: public class Supplier { public
I am using vb.net I have a Function in my class(class1) file namely save.
I have the following class which I am using to read in large amounts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.