Skip to content

$this is also defined in event handlers, as the event sender #8207

@mklement0

Description

@mklement0

The topic currently only mentions script-block-based ETS members and PowerShell classes.

A quick demonstration:

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$timer = new-object System.Windows.Forms.Timer
$timer.Add_Tick( { 
  param([object] $sender, [eventargs] $evArgs) 
  Write-Verbose -Verbose ('$this equals $sender? ' + [object]::ReferenceEquals($this, $sender));
  $form.Close() 
})
$timer.Interval = 10; $timer.Enabled = $true
$null = $form.ShowDialog()
$form.Dispose(); $timer.Dispose()

The above yields: VERBOSE: $this equals $sender? True


Document Details

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

Metadata

Metadata

Assignees

Labels

area-aboutArea - About_ topics

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions