If you don't explicitly type a variable, PoSH will try to treat it in a
way that makes sense for the context - meaning the type can change,
although typically once PoSH decides on a type, it sticks with it
unless the variable's content changes.
You can explicitly type:
[string]$abc = "Hello"
[int]$xyz = 123
$xyz = "World"
The last line would generate an error since "World" can't be coerced
into an integer. PrimalScript 4.1 uses static type declarations like
this to provide pop-up code hinting, too, since the PoSH variables have
the properties and methods of their underlying .NET Framework types.
On Dec 21, 8:40 pm, John Smith <JohnSm...@discussions.microsoft.com>
wrote:
> How are variables typed? It looks like the type of a variable can change
> every time it is assigned a new value. What are the rules? Why doesn't Line
> 4 produce an error? Thanks
>
> PS C:\> $abc = "1234567"
> PS C:\> $abc.GetType().ToString()
> System.String
> PS C:\> $abc = $abc / 1.2 # Line 4
> PS C:\> $abc
> 1028805.83333333
> PS C:\> $abc = 1234567
> PS C:\> $abc = $abc /1.2
> PS C:\> $abc
> 1028805.83333333
> PS C:\> $abc = "ABC"
> PS C:\> $abc = $abc /1.2
> Cannot convert value "ABC" to type "System.Double". Error: "Input string was
> not in a correct format."
> At line:1 char:14
> + $abc = $abc /1 <<<< .2