TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Notices

[string]$a = $null; $a -eq $null; $a -eq ""

Microsoft Windows Powershell


Reply
 
Thread Tools Display Modes
Old 13-06-2008, 08:54 PM   #1
Mat
Guest
 
Posts: n/a
[string]$a = $null; $a -eq $null; $a -eq ""

[string]$a = $null; $a -eq $null; $a -eq ""

False
True

So a string variable can't be set to $null (which causes problems trying to
work with .NET) - or is there another way to set a .NET string to null/$null?

Cheers,
-Mat
  Reply With Quote
Old 13-06-2008, 09:53 PM   #2
Tao Ma
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Hi Mat,

In the '[string] $a = $null', [string] is type attribute of the variable $a.
Unlike other languages, PowerShell will try to convert any object as long as
it is convertible to target type. So $null is converted into an empty string
object. Bruce Payette explains in his book Windows PowerShell In
Action.(page 142)

Is it possible to remove [string] here ? you can use '-is' to check
$variable type.

Tao Ma


"Mat" <> 写入消息新闻:...
> [string]$a = $null; $a -eq $null; $a -eq ""
>
> False
> True
>
> So a string variable can't be set to $null (which causes problems trying
> to
> work with .NET) - or is there another way to set a .NET string to
> null/$null?
>
> Cheers,
> -Mat



  Reply With Quote
Old 13-06-2008, 09:53 PM   #3
Roman Kuzmin
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

See also:


--
Thanks,
Roman Kuzmin


PowerShell and .NET in FAR Manager


"Mat" <> wrote in message
news:...
> [string]$a = $null; $a -eq $null; $a -eq ""
>
> False
> True
>
> So a string variable can't be set to $null (which causes problems trying
> to
> work with .NET) - or is there another way to set a .NET string to
> null/$null?
>
> Cheers,
> -Mat


  Reply With Quote
Old 13-06-2008, 11:56 PM   #4
Mat
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Thanks Roman, I added my vote.

Tao: I would expect that $null would convert to null (not "") becasue null
is a valid value for a string type.

Cheers,
-Mat

"Roman Kuzmin" wrote:

> See also:
>
>
> --
> Thanks,
> Roman Kuzmin
>
>
> PowerShell and .NET in FAR Manager
>
>
> "Mat" <> wrote in message
> news:...
> > [string]$a = $null; $a -eq $null; $a -eq ""
> >
> > False
> > True
> >
> > So a string variable can't be set to $null (which causes problems trying
> > to
> > work with .NET) - or is there another way to set a .NET string to
> > null/$null?
> >
> > Cheers,
> > -Mat

>

  Reply With Quote
Old 13-06-2008, 11:56 PM   #5
Mat
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Hi Tao, in my scenario I have a .NET object with a string property and I
want to set that property to $null but it apparently sets it to "" instead.
Cheers,
-Mat

"Tao Ma" wrote:

> Hi Mat,
>
> In the '[string] $a = $null', [string] is type attribute of the variable $a.
> Unlike other languages, PowerShell will try to convert any object as long as
> it is convertible to target type. So $null is converted into an empty string
> object. Bruce Payette explains in his book Windows PowerShell In
> Action.(page 142)
>
> Is it possible to remove [string] here ? you can use '-is' to check
> $variable type.
>
> Tao Ma
>
>
> "Mat" <> 脨麓脠毛脧没脧垄脨脗脦脜:...
> > [string]$a = $null; $a -eq $null; $a -eq ""
> >
> > False
> > True
> >
> > So a string variable can't be set to $null (which causes problems trying
> > to
> > work with .NET) - or is there another way to set a .NET string to
> > null/$null?
> >
> > Cheers,
> > -Mat

>
>
>

  Reply With Quote
Old 18-06-2008, 05:46 PM   #6
Jacob Saaby Nielsen
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Hey mat,

well that's perhaps the issue right there.

$null is not a valid value for anything - because $null is not a value.

$null is NOT equal to "".
$null is NOT equal to "0".

$null is more a state of non-existance, than it is an actual value.

"" is a value. It's a value that says "empty", and is of type string.
"0" is a value. It's a value that says "zero", and is of type int or char.
$null just tells you that there isn't a value, or that something doesn't
exist.

So setting a string value to $null, if you follow the logic of e.g. setting
a .NET object to $null (or Nothing),
should actually destroy the string variable, instead of setting it to nothing.

There is no logic reason, at least in my mind, why a string should ever be
$null. Because it it's $null, it's
either non-existant, or you have a variable with an unknown value. When you
have an empty string, you know
its value: Empty.

There's a lot of reason why a string should be just plain empty though.

I hope that contributed to understanding the difference between $null and...
well, everything else

Best Regards,
Jacob Saaby Nielsen


gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail

> Thanks Roman, I added my vote.
>
> Tao: I would expect that $null would convert to null (not "") becasue
> null is a valid value for a string type.
>
> Cheers,
> -Mat
> "Roman Kuzmin" wrote:
>
>> See also:
>>
>> 07821&SiteID=99
>> --
>> Thanks,
>> Roman Kuzmin
>>
>> PowerShell and .NET in FAR Manager
>> "Mat" <> wrote in message
>> news:...
>>
>>> [string]$a = $null; $a -eq $null; $a -eq ""
>>>
>>> False
>>> True
>>> So a string variable can't be set to $null (which causes problems
>>> trying
>>> to
>>> work with .NET) - or is there another way to set a .NET string to
>>> null/$null?
>>> Cheers,
>>> -Mat



  Reply With Quote
Old 19-06-2008, 01:53 AM   #7
Leo Tohill
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Still, in the world at large, there exist methods that take string parms that
have special meaning when null. Since we can't change the world, we have to
accommodate it. I think it is important for PS to support passing null to
string parms of methods.

"Jacob Saaby Nielsen" wrote:

> Hey mat,
>
> well that's perhaps the issue right there.
>
> $null is not a valid value for anything - because $null is not a value.
>
> $null is NOT equal to "".
> $null is NOT equal to "0".
>
> $null is more a state of non-existance, than it is an actual value.
>
> "" is a value. It's a value that says "empty", and is of type string.
> "0" is a value. It's a value that says "zero", and is of type int or char.
> $null just tells you that there isn't a value, or that something doesn't
> exist.
>
> So setting a string value to $null, if you follow the logic of e.g. setting
> a .NET object to $null (or Nothing),
> should actually destroy the string variable, instead of setting it to nothing.
>
> There is no logic reason, at least in my mind, why a string should ever be
> $null. Because it it's $null, it's
> either non-existant, or you have a variable with an unknown value. When you
> have an empty string, you know
> its value: Empty.
>
> There's a lot of reason why a string should be just plain empty though.
>
> I hope that contributed to understanding the difference between $null and...
> well, everything else
>
> Best Regards,
> Jacob Saaby Nielsen
>
>
> gmail: jacob DOT saaby
> hotmail (IM/LinkedIN/Facebook): same as gmail
>
> > Thanks Roman, I added my vote.
> >
> > Tao: I would expect that $null would convert to null (not "") becasue
> > null is a valid value for a string type.
> >
> > Cheers,
> > -Mat
> > "Roman Kuzmin" wrote:
> >
> >> See also:
> >>
> >> 07821&SiteID=99
> >> --
> >> Thanks,
> >> Roman Kuzmin
> >>
> >> PowerShell and .NET in FAR Manager
> >> "Mat" <> wrote in message
> >> news:...
> >>
> >>> [string]$a = $null; $a -eq $null; $a -eq ""
> >>>
> >>> False
> >>> True
> >>> So a string variable can't be set to $null (which causes problems
> >>> trying
> >>> to
> >>> work with .NET) - or is there another way to set a .NET string to
> >>> null/$null?
> >>> Cheers,
> >>> -Mat

>
>
>

  Reply With Quote
Old 19-06-2008, 03:58 AM   #8
Matthew Hobbs
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

"Jacob Saaby Nielsen" wrote:
> There is no logic reason, at least in my mind, why a string should ever be
> $null. [...]


Philosophy aside... System.String is a reference type and can be null. You
are talking about string values. System.String can refer to a value (a
string value) or not (null).
  Reply With Quote
Old 19-06-2008, 10:48 AM   #9
Karl Prosser[MVP]
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

It is a side effect of how powershell works, something to definately be
aware of, so you don't run into gotchas, but not a huge problem as you
can easily work around it in a variety of ways (i.e not force the
variable to be strongly typed as a string)

but it should be noted and documented a a gotcha

Leo Tohill wrote:
> Still, in the world at large, there exist methods that take string parms that
> have special meaning when null. Since we can't change the world, we have to
> accommodate it. I think it is important for PS to support passing null to
> string parms of methods.
>
> "Jacob Saaby Nielsen" wrote:
>
>> Hey mat,
>>
>> well that's perhaps the issue right there.
>>
>> $null is not a valid value for anything - because $null is not a value.
>>
>> $null is NOT equal to "".
>> $null is NOT equal to "0".
>>
>> $null is more a state of non-existance, than it is an actual value.
>>
>> "" is a value. It's a value that says "empty", and is of type string.
>> "0" is a value. It's a value that says "zero", and is of type int or char.
>> $null just tells you that there isn't a value, or that something doesn't
>> exist.
>>
>> So setting a string value to $null, if you follow the logic of e.g. setting
>> a .NET object to $null (or Nothing),
>> should actually destroy the string variable, instead of setting it to nothing.
>>
>> There is no logic reason, at least in my mind, why a string should ever be
>> $null. Because it it's $null, it's
>> either non-existant, or you have a variable with an unknown value. When you
>> have an empty string, you know
>> its value: Empty.
>>
>> There's a lot of reason why a string should be just plain empty though.
>>
>> I hope that contributed to understanding the difference between $null and...
>> well, everything else
>>
>> Best Regards,
>> Jacob Saaby Nielsen
>>
>>
>> gmail: jacob DOT saaby
>> hotmail (IM/LinkedIN/Facebook): same as gmail
>>
>>> Thanks Roman, I added my vote.
>>>
>>> Tao: I would expect that $null would convert to null (not "") becasue
>>> null is a valid value for a string type.
>>>
>>> Cheers,
>>> -Mat
>>> "Roman Kuzmin" wrote:
>>>
>>>> See also:
>>>>
>>>> 07821&SiteID=99
>>>> --
>>>> Thanks,
>>>> Roman Kuzmin
>>>>
>>>> PowerShell and .NET in FAR Manager
>>>> "Mat" <> wrote in message
>>>> news:...
>>>>
>>>>> [string]$a = $null; $a -eq $null; $a -eq ""
>>>>>
>>>>> False
>>>>> True
>>>>> So a string variable can't be set to $null (which causes problems
>>>>> trying
>>>>> to
>>>>> work with .NET) - or is there another way to set a .NET string to
>>>>> null/$null?
>>>>> Cheers,
>>>>> -Mat

>>
>>

  Reply With Quote
Old 19-06-2008, 06:47 PM   #10
Jacob Saaby Nielsen
Guest
 
Posts: n/a
Re: [string]$a = $null; $a -eq $null; $a -eq ""

Hey Matthew,

yes - I do of course mean the value of the string, thanks for correcting
me

Best Regards,
Jacob Saaby Nielsen


gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail

> "Jacob Saaby Nielsen" wrote:
>
>> There is no logic reason, at least in my mind, why a string should
>> ever be $null. [...]
>>

> Philosophy aside... System.String is a reference type and can be null.
> You are talking about string values. System.String can refer to a
> value (a string value) or not (null).
>



  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 03:11 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional