Skip to content

Conversation

@ddizh
Copy link
Contributor

@ddizh ddizh commented Apr 23, 2018

Changed for readability because 'displays 3 * * *' can confuse people

Changed for readability
@ddizh ddizh requested a review from rpetrusha as a code owner April 23, 2018 23:34
Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's certainly true that the example is confusing, @d-dizhevsky, it is the correct output. The following is the complete example (in the repo, it's been split into two noncompilable fragments):

Public Module Example
   Public Sub Main

        ' Variable n is a nullable type, but both m and n have proper values.
        Dim m As Integer = 3
        Dim n? As Integer = 2

        ' The comparison evaluated is 3>2, but compare1 is inferred to be of 
        ' type Boolean?.
        Dim compare1 = m > n
        ' The values summed are 3 and 2, but sum1 is inferred to be of type Integer?.
        Dim sum1 = m + n

        ' The following line displays: 3 * 2 * 5 * True
        Console.WriteLine(m & " * " & n & " * " & sum1 & " * " & compare1)

        ' Change the value of n to Nothing.
        n = Nothing

        Dim compare2 = m > n
        Dim sum2 = m + n

        ' Because the values of n, compare2, and sum2 are all Nothing, the
        ' following line displays 3 * * *
        Console.WriteLine(m & " * " & n & " * " & sum2 & " * " & compare2)
   End Sub
End Module

The output is:

3 *  *  *

I agree, though, that this is confusing. What I'd suggest is:

  • Replacing string concatenation with string interpolation in the Console.WriteLine method call.
  • Use the If() operator to display a string, such as "", if the variable is null. For example:
Console.WriteLine($"{m} * {If(n, "<null>")} * {If(sum2, "<null>")} * {If(compare2, "<null>")}")

@mairaw
Copy link
Contributor

mairaw commented Apr 24, 2018

We should first make the change here to point to the samples repo instead and then make the change there to the code. I'll create a new PR with the location change.

@mairaw
Copy link
Contributor

mairaw commented Apr 24, 2018

Created PR #5011 to change the snippet references. Changes to the samples could then be done directly to https://github.com/dotnet/samples/blob/master/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrNullableValue/VB/Class1.vb instead @rpetrusha.

@rpetrusha
Copy link
Contributor

Thanks for raising this issue, @d-dizhevsky. I've opened dotnet/samples#34 to address the issue. I'll close this PR now.

@rpetrusha rpetrusha closed this Apr 28, 2018
@ddizh ddizh deleted the patch-1 branch July 5, 2018 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants