![]() |
|
|||||||
| Notices |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Guest
Posts: n/a
|
Re: [string]$a = $null; $a -eq $null; $a -eq ""
Hey Matthew,
and P.S.: I would understand why a null string could be cast to an empty string. That would make sense. 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). > |
|
|
|
#12 |
|
Guest
Posts: n/a
|
Re: [string]$a = $null; $a -eq $null; $a -eq ""
How do you " not force the variable to be strongly typed as a string" when
the called method defines a parameter of type string? "Karl Prosser[MVP]" wrote: > 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 > >> > >> > |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |