Getting a std string into a String^

This is a discussion on Getting a std string into a String^ within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am trying to set the text of a textbox but textboxes only take String^ not std::string. I have ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    89

    Getting a std string into a String^

    Hi, I am trying to set the text of a textbox but textboxes only take String^ not std::string. I have a function to change a String^ into a std::string but not vice versa. According to this you can simply do: String^ s = somestring.c_str(). Here is my code followed by the error.

    Code:
    String ^rd;
    std::string read;
    
    read = "abcd";
    rd = read.c_str();
    error:
    Code:
    c:\documents and settings\ac251404\my documents\visual studio 2005\projects\project1\project1\Form1.h(590) : error C2440: '=' : cannot convert from 'const char *' to 'System::String ^'
            No user-defined-conversion operator available, or
            Cannot convert an unmanaged type to a managed type
    PS - using visual c++ 2005 express

    Thanks!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You could try the following, although I haven't tested:
    Code:
    rd = Marshal::PtrToStringAnsi(read.c_str());

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    89
    I got it working with the following:

    Code:
    rd = gcnew String(read.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21