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 at work we came across the following code (some of you might recognize
Today I had a strange encounter with gcc. consider the following code: float len[ELEM+1];
Today I was reading code from some very popular numerical libraries written in FORTRAN
Today I have faced very strange behavior of sqlite3_finalize statement. I am using sqlite
Today I was listening to the Hanselminutes show about .NET 3.5 SP1...What's inside ,
Today we have received some strange exceptions on our production website. They all have
Today I was writing some C code to sort an array of structs using
Today's problem in my code is kind of strange, and I could not reproduce
Today i StumbleUpon a strange cache behavior of Firefox 4 which is described bellow.
Today while I was writing some code for two methods that shows and hides

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.