question

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 asked MikhailFirsov-1277 answered

ToString vs Out-String

Hello,

Would anybody please explain to me why do these two "methods" work differently - I failed to find any information that would clarify this...
110217-00.png

Thank you in advance,
Michael

windows-server-powershell
00.png (25.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

Types commonly override the Object.ToString method to return a string that represents the object instance. For your above case, the type of $a1 is DateTime and the method is DateTime.ToSting that converts the value of the current DateTime object to its equivalent string representation. If the type doesn't override the ToSting method, it still calls the default Object.ToString() method. You can get the type using the GetType() method.

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

why do these two "methods" work differently

Because they are different :-)

Object.ToString is a .NET method and the objects decides what to return as string representation. If the object has nothing meanfull to return, then it returns it's name.

Out-String is a PowerShell cmdlet, which collects all string properties of an object and returns it as one accumulated string; see Exmaple 1.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered

"If the object has nothing meanfull to return, then it returns it's name." - the problem is I can't understand/predict when this method has or does not have something "meanfull to return"...

In other words I don't understand why in my example the "PredictFailure False" can not be cast to string :(

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

The explanation is given in the above documentation. The default implementation of the ToString method returns the fully qualified name of the type of the Object.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered MikhailFirsov-1277 edited

"The default implementation of the ToString method returns the fully qualified name of the type of the Object."- that's exactly that l don't understand: I don't think users would like to use ToString just to get the "the fully qualified name of the type of the Object" - conversion to string is expected, not just displaying the name of the object.

Another example:
110302-11.png

If the default implementation of the ToString method returns the fully qualified name of the type of the Object why in this case the real string is returned and not the name of the object as in the first example?




11.png (15.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered

Thank you all for your help!!!

Regards,
Michael

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.