![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Simple Types 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 |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Simple Types 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 |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| associated file types | Petey | Vista performance & maintenance | 3 | 02-10-2008 11:49 AM |
| types of the help objects. | klumsy@xtra.co.nz | PowerShell | 2 | 10-14-2006 10:28 AM |
| File Types | dev | Vista General | 4 | 07-16-2006 05:45 PM |
| File Types | =?Utf-8?B?VGVhbWhyYw==?= | Vista General | 0 | 07-07-2006 01:41 PM |