-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Surprising flow with Not Boolean? #32882
Copy link
Copy link
Closed
Labels
dotnet-visualbasic/svchelp wantedGood for community contributors to help [up-for-grabs]Good for community contributors to help [up-for-grabs]
Description
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 ModuleIt 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.
- ID: 7427bc30-4d66-b234-4537-200d4419cc24
- Version Independent ID: 786cb51a-cd8d-95a7-a11c-c39245a01e9d
- Content: Null-conditional Operators - Visual Basic
- Content Source: docs/visual-basic/language-reference/operators/null-conditional-operators.md
- Product: dotnet-visualbasic
- GitHub Login: @KathleenDollard
- Microsoft Alias: kdollard
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
dotnet-visualbasic/svchelp wantedGood for community contributors to help [up-for-grabs]Good for community contributors to help [up-for-grabs]