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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.