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 6617475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:42:33+00:00 2026-05-25T20:42:33+00:00

Today I’ve witnessed some very strange behavior in VB.NET. The code I am talking

  • 0

Today I’ve witnessed some very strange behavior in VB.NET. The code I am talking about is the following:

Option Strict On
Option Explicit On

Module Module1

    Sub Main()
        Dim thisShouldBeSet = False

        DoSomething(Function() thisShouldBeSet = True)

        If Not thisShouldBeSet Then
            Throw New Exception()
        End If

        Console.WriteLine("yaay")
    End Sub

    Sub DoSomething(action As Action)
        action.Invoke()
    End Sub
End Module

I know that the code in itself is flawed because I must use :

DoSomething(Sub() thisShouldBeSet = True)

Instead of:

DoSomething(Function() thisShouldBeSet = True)

But I find it very odd that even with Option Strict and Option Explicit On the compile allows me to compile this code.

What is even stranger is that when running the code the Action actually behaves like a Func(of Boolean).

Can anyone provide me with a valid explanation to why this is allowed in VB.NET? Is this a compiler / runtime bug?

  • 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-05-25T20:42:33+00:00Added an answer on May 25, 2026 at 8:42 pm

    Why should it not allow you to compile the code? thisShouldBeSet = True is a valid comparison, returning the value False (because thisShouldBeSet <> True). Remember that = in VB can mean both = (assignment) and == (comparison) in C#, depending on the context.

    To elaborate, Sub() thisShouldBeSet = True would be a shorthand for

    Sub Anonymous()
        thisShouldBeSet = True           ' Assignment
    End Sub
    

    whereas Function() thisShouldBeSet = True is a shorthand for

    Function Anonymous() As Boolean
        Return thisShouldBeSet = True    ' Comparison
    End Sub
    

    In VB, it is explicitly allowed to use a function with a return value as an Action. From the documentation of the System.Action delegate (highlighting by me):

    In C#, the method must return void. In Visual Basic, it must be defined by the Sub…End Sub construct. It can also be a method that returns a value that is ignored.

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

Sidebar

Related Questions

Today I experienced a very strange behavior with my XCode (using Version 4.2, iOS
Today at work we came across the following code (some of you might recognize
Today we have received some strange exceptions on our production website. They all have
Today I was listening to the Hanselminutes show about .NET 3.5 SP1...What's inside ,
Today while I was writing some code for two methods that shows and hides
Today i have a little problem containing a File Upload. First some Infos: Rubyversion:
Today, I was reading about structures in Let Us C (on Goodreads ). I
Today I made some tests and I am curious of the results. I made
Today i tested my website on seo tool site and found following error: Your
Today I ran into a very difficult TDD problem. I need to interact with

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.