TechTalkz.com Logo

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

Notices

Reply
 
Thread Tools Display Modes
Old 19-06-2008, 06:47 PM   #11
Jacob Saaby Nielsen
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

http://www.ijacob.info
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
Old 26-06-2008, 04:17 PM   #12
Leo Tohill
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
> >>
> >> http://www.ijacob.info
> >> 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:
> >>>> https://connect.microsoft.com/feedba...x?FeedbackID=3
> >>>> 07821&SiteID=99
> >>>> --
> >>>> Thanks,
> >>>> Roman Kuzmin
> >>>> http://code.google.com/p/farnet/
> >>>> PowerShell and .NET in FAR Manager
> >>>> "Mat" <Mat@discussions.microsoft.com> wrote in message
> >>>> news:F03AC132-C865-4FA3-8E54-FBD54362F6A6@microsoft.com...
> >>>>
> >>>>> [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
Reply

Thread Tools
Display Modes


Google
 


All times are GMT +5.5. The time now is 01:23 AM.


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