Search code examples
stringclassc++-cli

Get value of char variable into a ref class String in C++ (Visual Studio)


I'm now writing some codes for a button of my program on Visual Studio 2017. I have a char variable (for example char c = 't'), then I want the button label (which is button.Text) to be modified by changes of c. The button.Text is a ref class String properties.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    char c = 't';
    String^ xyz;

    button1->Text = xyz;
}

In VisualStudio 1

In VisualStudio 2

I have tried this solution but it couldn't work because the button.Text properties is a ref string class, not a string class. C++ convert from 1 char to string?

So can you help me for my problem? Thank you!


Solution

  • with accordance with @HansPassant advice:
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
      Char c = 't';// Remeber Char is managed one 
      button1->Text = gcnew String(c.ToString());
    }
      This way you can avoid marshalling and other costly interop operations
      unless you want to use managed and unmanaged code together.