SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 460 | Next

Steve Seguis

"Microsoft Windows Server 2008 Administration"

For example, look at this piece of code:
$a = 2
write-host ($a + 2)
We assigned the value 2 to the variable $a and then output to the screen the value
of $a + 2. As expected, this code sequence will result in the value 4 being displayed on
the screen.
Now look at this code:
$a = 2
$s = "Some string"
??¦some more code??¦
$a = "Steve"
??¦some more code??¦
write-host ($a + 2)
In this example, you create two variables: $a contains the value 2 while $s contains
the value "Some string". Now assume that you accidentally assigned the value "Steve"
to the variable $a when you meant to assign it to $s. (If you have a QWERTY keyboard,
the A key is right next S, so this kind of mistake can easily happen.) This time, the code
outputs "Steve2" instead of what we really intended, which was the value 4. If this was
in a large script, this error might be hard to find.
To avoid this kind of problem, you can typecast each variable, like so:
[int]$a = 2
[string]$s = "Some string"
??¦some more code??¦
$a = "Steve"
??¦some more code??¦
write-host ($a + 2)
You prefixed each variable as you first used it, with [int] and [string]. I told
Windows PowerShell that $a would hold an integer while $s would hold a string. If
You run this code, Windows PowerShell would spit out an error telling me that ???Steve???
cannot be converted to type System.Int32 (the long name for an integer).


Pages:
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
druga wojna światowa Free English grammar and study guid hotel jelenia góra Russian bride counter strike 1.6