Skip to content

Adding to hash table example doesn't work #1943

Closed
@derek-robinson

Description

@derek-robinson

Issue Details

The example code on how to add a hash table doesn't work correctly.
$hash = $hash.Add("Time", "Now")

To show this in action, we can run the below.

$hash = @{}
$hash.GetType()
$hash = $hash.Add("Time", "Now")
$hash.GetType()
$hash -eq $null

Results of running above

PS C:\> $hash = @{}
PS C:\> $hash.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object


PS C:\> $hash = @{}
PS C:\> $hash = $hash.Add("Time", "Now")

# Running gettype again after doing the add reveals that the variable is empty
PS C:\> $hash.GetType()
You cannot call a method on a null-valued expression.
At line:2 char:1
+ $hash.GetType()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\> $hash -eq $null
True

Version(s) of document impacted

  • Impacts 6 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Reason(s) for not selecting all version of documents

  • The documented feature was introduced in selected version of PowerShell
  • This issue only shows up in selected version of the document

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions