char * to String^
-
Hi all, I got a small trouble, If some one know how to do please tell me, many thanks. I want to assign szData value into textBox->Text, but two kind of variable different type. I used Visual Studio 2005, and developed VC++ language.
private: System::Windows::Forms::TextBox^ textBox; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { char* szData = "abcabcabcabc"; textBox->Text = //Content of szData variable. }
-
Hi all, I got a small trouble, If some one know how to do please tell me, many thanks. I want to assign szData value into textBox->Text, but two kind of variable different type. I used Visual Studio 2005, and developed VC++ language.
private: System::Windows::Forms::TextBox^ textBox; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { char* szData = "abcabcabcabc"; textBox->Text = //Content of szData variable. }
Do you need to use "char*" in managed code? How about this:
private: System::Windows::Forms::TextBox^ textBox;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
String ^str = "abcabcabcabc";
textBox->Text = str;
}or without the extra assignment...
private: System::Windows::Forms::TextBox^ textBox;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
textBox->Text = "abcabcabcabc";
}Mark
Last modified: 20mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi all, I got a small trouble, If some one know how to do please tell me, many thanks. I want to assign szData value into textBox->Text, but two kind of variable different type. I used Visual Studio 2005, and developed VC++ language.
private: System::Windows::Forms::TextBox^ textBox; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { char* szData = "abcabcabcabc"; textBox->Text = //Content of szData variable. }
-
textBox->Text = gcnew String(szData);
Yeah, This way I realy like. Can you introduce more about this. How can you find out this way? Do you have any tips for this kind? Sorry, my English very bad.Well it's quite simple: You can create String objects with passing char* to the String constructor. 'Text' is something like pointer. gcnew is something like new in c++ but a little bit smarter. So all we have to do is just create new String^ object with szData as value instead of "" ( String is initialized with "" string normally ).