Docs: Incorrect statement about passing Nothing as the value of a ParamArray parameter.

Created on 4 Oct 2018  Â·  9Comments  Â·  Source: dotnet/docs

In the section titled Calling a ParamArray we have this statement:

Nothing — that is, you can omit the ParamArray argument. In this case, an empty array is passed to the procedure. You can also pass the Nothing keyword, with the same effect.

The part in bold is incorrect as the following code shows:

Public Module Module1
  Private Sub foo(ParamArray v() As Object)
    If v Is Nothing Then
      Console.WriteLine("v is null")
    Else
      Console.WriteLine($"v is NOT null and has {v.Length} items.")
    End If
  End Sub

  Public Sub Main()
    foo()
    foo(Nothing)
    Console.ReadLine()
  End Sub
End Module

The quoted section correctly says that if you omit the ParamArray argument by writing foo(), an empty (i.e non-null) array is passed to the procedure. The code above prints v is NOT null and has 0 items. to show that this is correct.

The quoted section then incorrectly says "you can also pass the Nothing keyword, with the same effect". The effect is NOT the same because writing foo(Nothing) actually passes a NULL array instead of a zero-length/empty/non-null array as was the case when writing foo(). The code above prints v is null to prove this.

I suggest the quoted section should be modified to read similar to the following:

Nothing — that is, you can omit the ParamArray argument. In this case, an empty array is passed to the procedure. NOTE: If you explicitly pass the Nothing keyword, a null array is passed to the procedure and may result in a NullReferenceException if the called procedure does not check for this condition.

In fact, thinking about the above suggestion, I think there is a ton of code out there that is not expecting people to pass Nothing to a ParamArray parameter and would result in a NullReferenceException if they did. A quick look into the Rosyln repo shows this example:

  <Extension>
  Friend Sub VerifyNames(
               list As IVsSimpleObjectList2,
               itemPredicate As Func(Of IVsSimpleObjectList2, UInteger, Boolean),
               ParamArray names As String()
             )

    '<snipped code>

    For i = 0 To names.Length - 1
      'above expression 'names.Length' will throw if Nothing is passed to ParamArray parameter "names".
    Next

    '<snipped code>
  End Sub

Document Details

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

Area - Visual Basic Guide P1 doc-bug up-for-grabs

All 9 comments

@mairaw please take a look at this one when you get a chance!

Adding @KathleenDollard to comment on this issue.

@ericmutta Thank you for finding this.

@mairaw Eric is correct. Please make this change.

@ericmutta Have you seen this actually cause a bug in real code? I'm wondering if we need more here than the documentation fix - specifically do we need an analyzer to check for this? Feel free to follow up on this in vblang as it isn't a docs issue

@KathleenDollard many thanks for following up. Regarding the analyzer check I filed an issue on vblang to continue the discussion there :+1:

Hi @mairaw I think the waiting-on-feedback tag can be removed from this issue, right?

I am happy to create a PR for the change I suggested since @KathleenDollard is on board.

Let me know if I can proceed!

@ericmutta Yes, please make the change

Tag removed. Thanks for the ping.

I added the "up for grabs" label.

The suggested fix in the issue description is the correct fix.

Did a quick PR for this, using the suggested language verbatim and but keeping the link to the keyword,

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skylerberg picture skylerberg  Â·  3Comments

stanuku picture stanuku  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments

svick picture svick  Â·  3Comments

Eilon picture Eilon  Â·  3Comments