Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - passing variable by reference

Reply
 
Old 05-31-2006   #1 (permalink)
Marcel Ortiz


 
 

Re: passing variable by reference

[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\/\/" <no@spam.mow> wrote in message
news:OLB03DOhGHA.3924@TK2MSFTNGP03.phx.gbl...
> 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 System SpecsSystem Spec
Old 05-31-2006   #2 (permalink)
/\\/\\o\\/\\/


 
 

passing variable by reference

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 System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Variable within Object Reference Line VB Script
Passing dataset by reference between forms, what is the best approach? .NET General
Passing parameters to a cmdlet in a variable PowerShell
Passing ArrayList as reference to function PowerShell
Passing hash table by reference PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46