Windows Vista Forums

Simple Types
  1. #1


    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

  2. #2


    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

Simple Types problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
? types as an ampersand E Moberly Vista mail 1 27 Dec 2009
File Types-.jp2,jpc & jpe? Riptorn General Discussion 5 02 Aug 2009
types of software pacinitaly Software 3 06 May 2009
MIME Types Hillbilly .NET General 0 27 Dec 2008
Converting proxy generated types to original types Daniel Indigo 1 06 Jan 2007