locked
How to override ToString method in c++/cx?

    Question

  • Hi, All.

    I wanted to override ToString method in my ref class, but it didn't work, example code :

        public ref class ToStringTest
        {
        public:
            virtual String^ ToString() override
            {
                return "ToStringTest::ToString()";
            }
        };

    why compiler complain "method with override specifier 'override' did not override any base class methods" ? 

    Wednesday, September 28, 2011 4:15 PM

Answers

  • Object::ToString is not virtual and thus cannot be overridden. WinRT was designed with the idea that inheritance will only be used for Xaml controls. So the .NET-ish "Object is the root class" idea does not translate too well here. If you want a virtual ToString in your object hierarchy, either define your own base class and add a virtual ToString to it or define an interface (something like say IDescribable) and add a ToString to it and have your objects implement that interface.
    http://blog.voidnish.com

    Wednesday, September 28, 2011 5:14 PM

All replies

  • Object::ToString is not virtual and thus cannot be overridden. WinRT was designed with the idea that inheritance will only be used for Xaml controls. So the .NET-ish "Object is the root class" idea does not translate too well here. If you want a virtual ToString in your object hierarchy, either define your own base class and add a virtual ToString to it or define an interface (something like say IDescribable) and add a ToString to it and have your objects implement that interface.
    http://blog.voidnish.com

    Wednesday, September 28, 2011 5:14 PM
  • I'll add to Nishant's comment that I recommend naming that method something other than ToString() (like GetStringDescription(), etc.). Allowing the code below in your component may be confusing to a consumer:

     

    Object^ o = ref new MyComponent();

    auto m = ref new MyComponent();

    o->ToString(); // will call Object::ToString

    m->ToString(); // will call IDescribable::ToString

     

    Thx,
    Marian Luparu
    Visual C++

     

    Wednesday, September 28, 2011 10:51 PM
  • Thanks Nish, I just learn how to use XAML controls.
    Thursday, September 29, 2011 1:02 AM
  • Thanks Marian, I try to use ToString method in XAML controls, but now it can't work, is there any way to show the self defined class info?
    Thursday, September 29, 2011 1:19 AM
  • You can use XAML data binding.

    Look at the C++ Grid Application project template for an example of data binding (SampleDataSource.h).


    Thanks,
    Marian Luparu
    Visual C++

    Thursday, September 29, 2011 9:19 PM
  • The VS Express version didn't have Grid App template except Metro App template :(, but I find it in the VS Ultimate version, thanks.
    Friday, September 30, 2011 12:17 PM
  • The VS Express version didn't have Grid App template except Metro App template :(, but I find it in the VS Ultimate version, thanks.

    That's odd. I have the Express edition too and the C++ templates have a Grid project template.
    http://blog.voidnish.com
    Friday, September 30, 2011 2:20 PM
  • The VS Express version didn't have Grid App template except Metro App template :(, but I find it in the VS Ultimate version, thanks.

    That's odd. I have the Express edition too and the C++ templates have a Grid project template.
    http://blog.voidnish.com
    Sorry,it was my fault.I was so exciting to find the grid project template in Ultimate edition that forget to check the Express Edition:(.
    • Edited by niqil Friday, September 30, 2011 5:13 PM
    Friday, September 30, 2011 5:12 PM