about_Ref | Easy Powershell 2.0 Reference (original) (raw)

TOPIC
about_Ref

SHORT DESCRIPTION
Describes how to create and use a reference Variable type.

LONG DESCRIPTION
You can use the reference Variable type to permit a method to change the
value of a Variable that is passed to it.

When the [ref] type is associated with an object, it returns a reference
to that object. If the reference is used with a method, the method can
refer to the object that was passed to it. If the object is changed within
the method, the change appears as a change in the value of the Variable
when control returns to the calling method.

To use referencing, the parameter must be a reference Variable. If it is
not, an InvalidArgument exception is thrown.

The parameters used in method invocations must match the type required
by the methods.

Examples:

PS> Function swap([ref]$a,[ref]$b)
>> {
>> a.value,a.value,a.value,b.value = b.value,b.value,b.value,a.value
>> }

PS> $a = 1
PS> $b = 10
PS> a,a,a,b
1
10
PS> swap ([ref]$a) ([ref]$b)
PS> a,a,a,b
10
1

PS C:\ps-test> Function double
>> {
>> param ([ref]$x) x.value=x.value = x.value=x.value * 2
>> }

PS C:> $number = 8
PS C:> $number
8
PS C> double ([ref]$number)
PS C> $number
16

The Variable must be a reference Variable.

PS C:\ps-test> double $number
double : Reference type is expected in argument.
At line:1 char:7
+ double <<<< $number

SEE ALSO
about_Variables
about_environment_variables
about_functions
about_script_blocks

Post navigation