PS – parameter by reference

Many developers know them – parameter by reference. By this way the variables will not be duplicated. A pointer is created which points to the actual variable. If you modify this referenced parameter you will modify the referenced variable. So you can make changes to many parameters and will not create a lot of functions with returnvalues.

[string]$Global = ''


function TestRefFunction([ref]$Param)
{
    [string]$Param.Value = '5'
    $Param | Out-Default
}

TestRefFunction([ref]$Global)

$Global | Out-Default

By this example the transmitted parameter “$global” is a reference. So all changes to it in the function will also be written into the global variable. In this example so 2 times a “5” will be given out.

~David

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s