Can't override ToString().

I have the following class:

#pragma once

using namespace System;

namespace AutoTickets

{

ref class Model : System::Object

{

public:

Model(void) : M_model("")

{}

Model(String^ model) : M_model(model)

{}

property String^ model

{

String^ get() { return M_model; }

void set(String^ p_model) { M_model = p_model; }

}

virtual String^ ToString() override

{

return M_model;

}

private:

String^ M_model;

};

}

For some reason ToString() is not being overriden.

I have other classes that do the same thing and work fine. When I call ToString on an object of this type all I get is "AutoTickets.Model".

I'm not sure if I'm doing something wrong here. I'm relatively new to .NET.

I'd appreciate any help with this. I made the class explicitely inherit from System::Object just to make sure. Either way Model::ToString() is not being called. I put a debug MessageBox::Show() to see if it was being called and it isn't.



Answer this question

Can't override ToString().

  • Rajiv Bose

    visual c++ 2005 express.

    Weird thing is that I have three other classes that override ToString() and they work well. I can't seem to figure out what I'm doing wrong here. I've been looking at it for about two hours now. Also that's all there is to the class.

    Thanks either way.


  • Greyeye

    > Do you have any idea of what would cause something like this in the project configuration I can't think of anything that would cause something like this.

    I can't think of anything other than a tip to narrow it down. On each project, go to the C/C++ settings, Command line, and under "Additional options" add "/Bd." This will output all the compiler settings, including assembly references that the projects make.

    > Once again, I'm relatively new to .NET and programming under windows.

    Looks like you're doing fine so far! Just a weird bump in the road, that's all. If it ends up being a bug in VC++, then you get the chance to tell Microsoft off and report a bug at the product feedback site. :)

    Brian


  • MarkMadd

    I am not able to reproduce your problem. I tested this with:

    int main(array<System::String ^> ^args)
    {
    AutoTickets::Model^ mymodel =
    gcnew AutoTickets::Model( L"test" );
    System::Object^ obj = mymodel;
    Console::WriteLine(obj->ToString());
    return 0;
    }

    obj->ToString() calls into Model::ToString() as expected.

    Which version of Visual Studio are you using

    Brian


  • William Joyce

    It appears it was a problem with VC++. I excluded the file from the project and readded it and even though I had (unsuccessfully apparently) to save the file it did not save the file. So the version of the file without the ToString() override function wasn't being compiled.

    Either way I though the compiler would compile the file currently being edited even if it wasn't saved.

    There was another rather nasty bug that I had a couple of days ago. I posted here. Also someone appeared to have the same problem before. It happens when you create a project and move the project file to another computer.

    For some reason the compiler looks for a file named (IIRC) comlib.hlp.

    Apparently it had something to do with pre-compiled headers. Since I don't need pre-compiled headers I disabled them from the project properties and the problem was gone.

    Well thanks for the time on trying to help.

    bye now :)

    Alan


  • Shashi.kiran

    Try taking the steps I've taken to see if you can pin down one end of the working v. not-working spectrum.  Create a new CLR Console project, copy/paste the code you posted here, and then replace main() with the version I provided and test it.

    If this works, then you have a project to compare differences against.

    Brian

     


  • Greig

    So I did that and it worked fine.

    Do you have any idea of what would cause something like this in the project configuration I can't think of anything that would cause something like this.

    Once again, I'm relatively new to .NET and programming under windows.

    Thanks

    Alan.


  • Can't override ToString().