Friday, April 30, 2010

An easy way to use a type constraint in Windows PowerShell

If you’re going to accept a parameter for a function, you have to use the type constraint in order to ensure that the function receives the correct type of Data. If you’re ready to do this, just place the desired Data type alias inside square brackets in front of Input Parameter. This action can constrain the Data type and prevent the entry of an incorrect type of Data.

Allowable type shortcuts are shown here:

[int] 32-bit signed integer
[long] 64-bit signed integer
[string] Fixed-length string of Unicode characters

[char] Unicode 16-bit character
[bool] True/false value
[byte] 8-bit unsigned integer
[double] Double-precision 64-bit floating point number
[decimal] 128-bit decimal value
[single] Single-precision 32-bit floating point number
[array] Array of values
[xml] Xmldocument object
[hashtable] Hashtable object (similar to a Dictionary object)