Re: this.toString

From: Miguel De Anda (_sodamnmad__at__hotmail_._com_)
Date: 10/18/03


Date: Fri, 17 Oct 2003 22:16:21 GMT


"Shaun" <shaun@mania.plus.com> wrote in message
news:UcYjb.11806$kA.2975528@wards.force9.net...
> Hi,
>
> I am a java beginner. Could someone please explain why calling the this
> keyword makes an implicit call to Objects toString method as in the
> following code example taken from Java How To Program?
>

<snip>

> // return String representation of Point4 object
> public String toString()
> {
> return "[" + getX() + ", " + getY() + "]";
> }
>

Since you overwrote the toString(), it shouldn't make the call to the
Object's toString(). It should end at the Point's toString(). If you didn't
have the toString() method there, it would return the string that the Object
class returns, usually funky stuff. I think the question you are really
asking is why the toString() method gets called on the ("..." + this) part.
The simplest reason I can think of is because you are implicitly trying to
convert the Point object to a String object. To do this, it uses the
toString() method because that's just the way they chose to do it. They
could have picked public String converThisObjectToAString() but I guess they
figured it was too long.



Relevant Pages

  • Re: Databinding to a combo box: Newbie
    ... Override the ToString function to return the 'look' that you want the ... The combobox will display the ToString output but the Selected item ... public struct stTuple ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ToString Debug vs Display strategies & preferences
    ... IFormattable implementation as well as ToString() override. ... public string FirstName ... public string ToString(string fmt, IFormatProvider fp) ... How about generalized debugging ToString stratagies? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: this.toString
    ... >> I am a java beginner. ... >> keyword makes an implicit call to Objects toString method as in the ... > As far as I can see there won't be a call to Object's toString() method. ... I interpreted "Object's toString method" as "the toString method, ...
    (comp.lang.java.programmer)
  • Re: property inference
    ... public string FirstName ... public override string ToString() ... if you do this type of inference there's no access to the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET
    ... >public string getEmail ... which you try to cast by calling .ToString- but you're calling that ... can't just call a .ToString() on either. ... VB.NET probably shields you from those programming errors by some ...
    (microsoft.public.dotnet.languages.csharp)