Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Simple Types

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-21-2006   #1 (permalink)
John Smith
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 SpecsSystem Spec
Old 12-24-2006   #2 (permalink)
Don Jones [MVP]
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51