![]() |
|
|||||||
| Notices |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
#2 |
|
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 |
|
|
|
#3 |
|
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 |
|
|
|
#4 |
|
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 > |
|
|
|
#5 |
|
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 > > > |
|
|
|
#6 |
|
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 |
|
|
|
#7 |
|
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 > > > |
|
|
|
#8 |
|
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). |
|
|
|
#9 |
|
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 >> >> |
|
|
|
#10 |
|
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). > |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |