Skip to content

Surprising flow with Not Boolean? #32882

@vbjay

Description

@vbjay
Module Module1

    Sub Main()

        Dim cat = "The cat is in the hat"
        Dim msgs() As String = {Nothing, cat, "The other error", ""}



        For Each strMessage In msgs
            Dim hasCat = strMessage?.Contains(cat)


            Console.WriteLine($"{strMessage}:{strMessage?.Contains(cat)}: hasCat hasvalue:{hasCat.HasValue} and hascat value: {hasCat.GetValueOrDefault}")

            If Not strMessage?.Contains(cat) Then
                Console.WriteLine("Inside if original")

            End If

            If Not If(strMessage?.Contains(cat), False) Then
                Console.WriteLine("Inside if mod")

            End If


        Next
        Console.WriteLine("Press a key...")
        Console.Read()

    End Sub

End Module

It needs to be clear that if Not someStr?.Contains("some string") or any other value that evaluates as boolean? ( and has the value of nothing or HasValue=false) would actually run else block in if to prevent any confusion. The evaluation follows the sql evaluation where null/nothing does not = another null/nothing.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions