passing variable by reference

M

Marcel Ortiz

[something] is just text when you're using it as an argument. So you get
the following:

PS>function foo($bar) { $bar }
PS>$x = 34
PS>foo [ref]$x
[ref]34

There's a bit about this in the help for about_parsing. Its the stuff about
expression mode vs argument mode.

It *would* be nice if we got better help for stuff like this though. I've
been thinking of writing a script to run in the prompt function that would
check if you just had an error in the previous line, do some regex mathing
and suggest stuff. I was mainly thinking of doing it because I keep trying
to use the old cmdlet names. :)
The script could detect stuff like this as well.

Anyway, its just an idea, I thought I'd put out there.

- Marcel


"/\/\o\/\/" <[email protected]> wrote in message
news:[email protected]
> while answering a powershell question in m.p.w.s.s,
>
> I stubled to this :
>
> MowPS>$foo
> bar
> MowPS>function barFoo ([ref]$foo){$foo.value = 'foo'}
>
> I need to use parentheses to pass a ref variable to get it parsed,
> and this is not really clear from the error :
>
> MowPS>barfoo [ref]$foo
> barFoo : Reference type is expected in argument.
> At line:1 char:7
> + barfoo <<<< [ref]$foo
>
> MowPS>barfoo ([ref]$foo)
> MowPS>$foo
> foo
>
> gr /\/\o\/\/
 

My Computer

O

/\\/\\o\\/\\/

while answering a powershell question in m.p.w.s.s,

I stubled to this :

MowPS>$foo
bar
MowPS>function barFoo ([ref]$foo){$foo.value = 'foo'}

I need to use parentheses to pass a ref variable to get it parsed,
and this is not really clear from the error :

MowPS>barfoo [ref]$foo
barFoo : Reference type is expected in argument.
At line:1 char:7
+ barfoo <<<< [ref]$foo

MowPS>barfoo ([ref]$foo)
MowPS>$foo
foo

gr /\/\o\/\/
 

My Computer

Top