2

Why does this not work?

PS deployables:\> $host.ui.rawui.windowsize.width
170
PS deployables:\> $host.ui.rawui.windowsize.width = 100
PS deployables:\> $host.ui.rawui.windowsize.width
170

But this does?

PS deployables:\> $host.ui.rawui.windowsize.width
170
PS deployables:\> $newsize = $host.ui.rawui.windowsize
PS deployables:\> $newsize.width = 100
PS deployables:\> $host.ui.rawui.windowsize = $newsize
PS deployables:\> $host.ui.rawui.windowsize.width
100

I'm not sure if I'm misunderstanding PowerShell or .NET property assignment, could someone shed some light on this for me?

Nick Garvey
  • 2,980
  • 24
  • 31

1 Answers1

2

Ugh, I can't believe PowerShell defined setters on these structs. Structs in .NET should generally be immutable to avoid confusion like you're seeing above. See this SO question for more details. Structs are passed around by value. When you modify the WindowSize struct, you are modifying a copy of it. When you assign the whole struct e.g. using $newsize it works because you're assigning a whole new struct value to the WindowSize property.

Community
  • 1
  • 1
Keith Hill
  • 194,368
  • 42
  • 353
  • 369