2

Say I've a psobject like this :

$o=New-Object PSObject -Property @{"value"=0}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
    echo "the square root of $($this.value) is $([Math]::Round([Math]::Sqrt($this.value),2))"
} -inputObject $o

Is it possible to attach an event so that the method Sqrt() is executed when the value attribute change ? ie :

PS>$o.value=9

will produce

the square root of 9 is 3

update

As per @Richard answer this is the working recipe :

$o=New-Object PSObject -property @{"_val"=1}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
    write-host "the square root of $($this._val) is $([Math]::Round([Math]::Sqrt($this._val),2))"
} -inputObject $o


Add-Member -MemberType ScriptProperty -Name 'val' -Value{ $this._val }  -SecondValue { $this._val=$args[0];$this.Sqrt() } -inputObject $o
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103

1 Answers1

5

Rather than making value a NoteProperty make it a ScriptProperty, this includes defining separate get and set methods that are called rather than directly modifying a field.

$theObject | Add-Member -MemberType ScriptProperty -Name 'Value' 
                        -Value{ $this._value }
                        -SecondValue { $this._value = $args[0]; $this.Sqrt }

(Value defines the get method, SecondValue the set.)

Note as PowerShell doesn't provide any ability to encapsulate data, the underlying field is still accessible to callers. Coding a custom type in C# (or other .NET language) and use of Add-Type can avoid this, but is unlikely to be worth it unless you really have callers who will not follow the rules.

Second Issue

In a ScriptProperty there is no output pipe (any output is thrown away), so echo (being an alias for Write-Output) won't do anything useful. Replacing it with Write-Host works. In general side effects in a property get or set (including output) are poor practice (there is an expectation of low overhead when using them).

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
Richard
  • 106,783
  • 21
  • 203
  • 265
  • You've defined `val` in terms of itself. The underlying field has to be a different name (notice the `_` prefix I used). – Richard Sep 18 '13 at 09:01
  • Oups, thanks. But, now the underlying field is modified but the method is not executed. I've updated my post. – Loïc MICHEL Sep 18 '13 at 09:15
  • do you mean this is a bad practice? – Loïc MICHEL Sep 18 '13 at 16:10
  • @Kayasax: Using a property for side effects: Yes. (However I would say "poor" rather than "bad": there are far worse practices out there :-(.) – Richard Sep 18 '13 at 18:00
  • OK. But in this case it will be made on purpose (I was thinking about something like onChange or onSubmit events in javascript) – Loïc MICHEL Sep 18 '13 at 19:02
  • thanks again this allowed me to do lazy loading un my profile :)http://stackoverflow.com/questions/33751430/lazy-loading-in-powershell – Loïc MICHEL Dec 04 '15 at 15:10