CLR Assign user input to string variable

I have this empty variable in my code:
 
string input;


I have a TextBox form named "input1" with ReadOnly set to false.
I tried to assign a property of it to my variable:
373
374
375
private: System::Void action(System::Object^ sender, System::EventArgs^ e) {
	input = input1->Text;
};


I expect the input variable to have the same value as the form's text when the action function is fired, but I receive these errors when running it (both are for line 374):

E0349	no operator "=" matches these operands		
C2679	binary '=': no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)		

How do I fix this? Am I supposed to convert?
Last edited on
And what is the type of input1->Text? It's a System::String^, right?

The problem is they're two different types. Your string input; is presumably a std::string input;, which is part of the C++ standard library, and a separate type from Microsoft's CLR/.NET string.

Now, I haven't actually done this myself, but a search brings up the following link:
https://stackoverflow.com/questions/1300718/c-net-convert-systemstring-to-stdstring
Directly pasting Colin's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::string standardString = context.marshal_as<std::string>(managedString);

    return 0;
}

(Obviously remove the stdafx.h if your environment isn't using that)
Last edited on
Topic archived. No new replies allowed.