Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Calling a function

2009-10-04

The parenthesis are important when you are calling a function in PowerShell. The parenthesis tells PowerShell that you intend to call the method, not acquire a reference to it. This code illustrates this.

$random = New-Object System.Random
$response1 = $random.next
$response2 = $random.next()

The $response1 variable will contain a reference to the function next, and the $response2 variable will contain an integer value, like 1611459187. The type of $response1 is System.Management.Automation.PSMethod and the type of $response2 is int32. If you want to use the reference to the function, use its Invoke method:

Write-Output $response1.Invoke()

To call static function, type the path to the function (namespaces and classes) followed by a double colon (::), and then the name of the function. Parenthesis are required, even if the function doesn’t take any parameters.

[System.Reflection.Assembly]::Load
("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[System.Windows.Forms.MessageBox]::Show("Hello!")

Categories: PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips