Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typed variables

2 views
Skip to first unread message

Larry__Weiss

unread,
Nov 19, 2009, 6:30:58 PM11/19/09
to

I just now noticed there is a big difference in coding
$i = [int] 9.99
and
[int] $i = 9.99

PS C:> $i = [int] 9.99
PS C:> $i
10
PS C:> $i = 9.99
PS C:> $i
9.99

PS C:> [int] $i = 9.99
PS C:> $i
10
PS C:> $i = 9.99
PS C:> $i
10

How would I take away the type binding from $i without deleting the variable
once I associate a type with it?

- Larry


RichS [MVP]

unread,
Nov 20, 2009, 6:19:01 AM11/20/09
to
you can change the type

[double]$i = 9.99

Typing as an integer and then giving it a non-integer value could cause
issues with your logic
--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Nov 20, 2009, 11:11:25 AM11/20/09
to
I do understand that I can assign the variable a constraint to another type, but
what I'm wondering is how to remove all type constraints from it.

- Larry

RichS [MVP]

unread,
Nov 20, 2009, 1:56:01 PM11/20/09
to
I think you'll find that all PowerShell variables carry a type due to it
being based on .NET

Try this

$i = 3
$s = "3"
$i | gm
$s | gm

types are assigned implicitly by PowerShell when it recognises the data type.

Even if you do something like

$w = Get-WmiObject win32_networkadapter
$w | gm

you will see that $w has a type

PowerShell can change the type of a variable implicitly or explicitly but
everything will have a type

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Nov 20, 2009, 4:01:26 PM11/20/09
to
Yes, but my question is with regards to a strong typed constraint that gets
imposed on a variable after you execute something like

[int] $i = 10

Thereafter PowerShell will not implicitly change the type association without
explicit direction.

Up until the type was strongly bound to the variable, PowerShell would mutate
the type of the variable as needed.

So, there must be something like a "now-strongly-bound" Boolean like property
associated with a variable.

Consider

PS C:> [int] $i = 1; gi variable:i | fl *
PSPath : Microsoft.PowerShell.Core\Variable::i
PSDrive : Variable
PSProvider : Microsoft.PowerShell.Core\Variable
PSIsContainer : False
Name : i
Description :
Value : 1
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {System.Management.Automation.ArgumentTypeConverterAttribute}

PS C:> $x = 1; gi variable:x | fl *
PSPath : Microsoft.PowerShell.Core\Variable::x
PSDrive : Variable
PSProvider : Microsoft.PowerShell.Core\Variable
PSIsContainer : False
Name : x
Description :
Value : 1
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}

So, that collection bound to the Attributes property makes all the difference
between a variable strongly type bound and one that is not strongly type bound.

So, how does one restore the value of Attributes back to {} ?

Martin Zugec

unread,
Nov 25, 2009, 3:31:18 PM11/25/09
to

Hi Larry,

I am afraid there is no way to reset it without deleting variable :(

Martin

"Larry__Weiss" <l...@airmail.net> wrote in message
news:eBisaBXa...@TK2MSFTNGP05.phx.gbl...

Larry__Weiss

unread,
Nov 25, 2009, 4:12:01 PM11/25/09
to
Thanks!

Looks like that is a "read-only" property from our perspective.

- Larry

Martin Zugec

unread,
Nov 25, 2009, 4:45:26 PM11/25/09
to

Not only from our perspective, but also according to MSDN :(

http://msdn.microsoft.com/en-us/library/system.management.automation.psvariable.attributes(VS.85).aspx

Martin

"Larry__Weiss" <l...@airmail.net> wrote in message

news:udz#xPhbKH...@TK2MSFTNGP06.phx.gbl...

0 new messages