Hi,

I'm a real newbie at C++, and I'm having a ton of trouble.
You see, What I need to do is convert an std::string into a system::string^ so I can display it in a textbox, and I haven't found any way of doing so :icon_confused: . I always get the compiler error 'cannot convert parameter 1 from 'std::string' to 'system::string ^'


I've searched on google, and they give a million ways to do the opposite of what i want to do. I'm using Visual C++ 2008 with .NET Framework 3.5. Here's my code:

int method;
int x = 53;
int y = 85;

private: System::Void btnCalc_Click(System::Object^  sender, System::EventArgs^  e) {
if(this->radioButton1->Checked == true)
  if(this->radioButton2->Checked == false)
    if(this->radioButton3->Checked == false)
      if(this->radioButton4->Checked == false)
      {
       method = x + y;
       
       std::string txtboxstring; // Converts 'method' (int)
       std::stringstream out;   //  into std::string
       out << method;
       
       //NEED TO CONVERT STD::STRING TO SYSTEM::STRING ^
       
        this->txtresult->Text = txtboxstring;
							
       }
}

Well, that's it. Thanks in advance to anyone who can help.

btw, yes, I know there are capitals on system::string, but I can't cuz it just gives the :S Icon.

This isn't really a C++ question, more of a C++/CLI question, which is a separate language.

But if you looked in the System.String documentation, you'd see the answer: http://msdn.microsoft.com/en-us/library/ezh7k8d5.aspx

Your real problem is that you're using std::string in the first place -- if you're going to be creating a System::String^, create it directly.

Ok, sarehu, thanks for the advice...

Your real problem is that you're using std::string in the first place -- if you're going to be creating a System::string^, create it directly.

I know I sound like a total retard, but I can't find any way around it because System::string has no stringstream, or any ostringstream.

What I'm asking for is how to correctly code the syntax. (I've tried tons of different ways, and they don't work at all.

>I can't find any way around it because System::string
>has no stringstream, or any ostringstream.
You probably don't need a string stream. System::string is a surprisingly robust class that can probably handle what you want to do in a slightly different way. Perhaps if you described the problem you want to solve, someone can suggest a way to do it using System::string directly.

Put this void MarshalString inside your namespace.
This used for possible conversions.
Once you have done this, you can convert a std::string to System:: String^ like the code below:

void MarshalString ( System::String^ s, std::string& os )
{   
using namespace System::Runtime::InteropServices;   
const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();   
os = chars;   
Marshal::FreeHGlobal(IntPtr((void*)chars));
}

Conversion:

std::string Hello1 = "Hello1";
String^ Hello2 = "";

//Convert the string
MarshalString(Hello2, Hello1);

//Hello2 now has the content of Hello1 and you could put Hello2 to a ex: textBox1->Text = Hello2;

Hope it helps


Ok, sarehu, thanks for the advice...

I know I sound like a total retard, but I can't find any way around it because System::string has no stringstream, or any ostringstream.

What I'm asking for is how to correctly code the syntax. (I've tried tons of different ways, and they don't work at all.

Thanks Jennifer,

It now compiles well, but the only problem is that if you click the button to display the text in the textbox, I get the error messagebox:

Debug Assertion Failed!

Expression: Invalid null pointer

and by clicking the 'retry' button, this file showed up (assembly debug info):

--- f:\dd\vctools\crt_bld\self_x86\crt\src\stdthrow.cpp ------------------------
00000000 push edi
00000001 push esi
00000002 push ebx
00000003 mov edi,ecx
00000005 mov ebx,edx
00000007 cmp dword ptr ds:[002B2F58h],0
0000000e je 00000015
00000010 call 78B03057
00000015 push 2
00000017 push ebx
00000018 push dword ptr [esp+18h]
0000001c push 0
0000001e push edi
0000001f push 4179C0h
00000024 call FF4D477C
00000029 add esp,18h
0000002c mov esi,eax
0000002e cmp esi,1
00000031 jne 00000038
00000033 call 78B04D52
00000038 nop
00000039 pop ebx
0000003a pop esi
0000003b pop edi
0000003c ret 4

take note that the 'nop' is blank... which caused the error. How can I fix this?

That's what you get for taking the path of least resistance. Now why don't you tell us what you're trying to do so we can suggest a good solution using System::string?

Narue,

Perhaps if you described the problem you want to solve, someone can suggest a way to do it using System::string directly.

Well, heres what I'm trying to do all in all.

I'm making an experimental 'algebra' calculator, Because it has to store data in variables, and it assigns data to those variables from textboxes. I need these types of operations in many windows forms applications.

First, the user can click 'set X' and they set it with a number, therefore assigning a String^ from a textbox. Then they click 'set Y', which does the same thing. Then, they select in 4 radiobuttons +, -, x, or /. There's then a button that says 'calculate', and the output should come out on the result textbox. (txtresult)

Here are the variables:

String^ xvalue;
String^ yvalue;
String^ resultstr;

int method;
int x;
int y;

Here's what I've successfully done so far:

Created the main window,
Set X assigns the user-created value to String^ xvalue; (Textboxes can't assign ints, as
Set Y assigns the user-created value to String^ yvalue; you know)

I then convert String^ xvalue and String^ yvalue to ints:

__int32 x =  System::Int32::Parse(xvalue);
__int32 y =  System::Int32::Parse(yvalue);

That way, when the user clicks 'calculate' it will check which button the user pressed, then calculate the method (I'll use the + action as an example)

if(this->radioButton1->Checked == true)
 if(this->radioButton2->Checked == false)
  if(this->radioButton3->Checked == false)
   if(this->radioButton4->Checked == false)
   {
    method = x + y;

Then, it gets to the part where I'm having trouble with. In order to display 'method' in a textbox, one would have to convert it to string, and here's the only way I can find to do so:

std::string txtboxstring;
std::stringstream out;
out << method;

Which compiles properly, however I can't convert std::string into system::string, and I need it to display in the txtresult textbox. I can't just say:

txtresult->text = txtboxstring;

Because I always get the compiler error: cannot convert parameter 1 from 'std::string' to 'System::string ^'.

So what I'm asking is, could someone help me find a way around stringstream and use just system::string^, or convert std::string to system::string^.

Can you post the code that you have written inside of your buttoncontrol. This will help a bit as a start to see if it is correctly written.

/J

How about removing the string streams and simply doing this?

txtresult->text = Convert::ToString ( method );

How about removing the string streams and simply doing this?

txtresult->text = Convert::ToString ( method );

I tried that, and I get the exception error on the compiler: 'cannot access private member declared in class 'System::Windows::Forms::Control'.

>cannot access private member declared in class 'System::Windows::Forms::Control
Well, the error message is pretty freaking clear about what the problem is. Are you not capable of figuring out why you can't access txtresult? It seems like you're trying to get to the Text property of a control on a different form, is that the case?

#include <iostream>

using namespace System;
using namespace std;

void main()
{
String^ newString;
char* oldString = "Hello World";

for(int i = 0; i < (int)strlen(oldString); ++i)
newString += (wchar_t)oldString;

Console::WriteLine(newString);

cin.get(); // to pause output screen to read Hello World


}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.