1

Consider this simple code:

$myObject = New-Object PSCustomObject
$myObject | Add-Member -MemberType ScriptMethod -Name 'Multiply' -Value { $x=Read-Host; $x = $x * 10; Write-Host "$x" } 
$myObject.Multiply()

Entering 1 returns 1111111111 but I would expect the value to be 10.

What is wrong?

1 Answer 1

2

Thats because $x is a string as you can verify by calling $x.GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

You can fix that by casting the variable to a [double]:

$x=Read-Host; $x = [double]$x * 10; Write-Host "$x"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.