Hello Everyone.
I am creating this Windows Forms Application using Visual Studio.
Basically, in order to do what I want to do, I need to use an integer, and that integer needs to be converted into a string in order to change the label text, when pushing the button. However, using static_cast, like I did in the code below gives me " error C2061: syntax error : identifier 'string' ". :( I have included <string> on top of the file.
What shall I do in order to fix this problem and make this run? Any suggestions?

Thanks in advance. :)

#pragma once
#include <string>


namespace testingforms {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
///////////////////////////////////////
// Everything that is created by default
// when creating a Windows Forms Application
// I just deleted it, I don't think it is that much of a concern.
//////////////////////////////////////


	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
			 {
			
				 int a;
				 a=5;
				 label2->Text = static_cast<string>(a); //right here
			 }


}

Recommended Answers

All 6 Replies

Not sure if in Visual_Basic works like this, but in Borland C++ Builder worked just fine

void __fastcall TForm1::Button3Click(TObject *Sender)
{
int k;
k=5   ;
Label1->Caption = k;
}

Looks like ye' need to perform a good ol' int-to-string conversion. Today we'll be creating an 'ostringstream' object derived from the <sstream> library in order to facilitate our int-to-string needs:

#include<sstream>
#include<string>
#include<iostream>

using namespace std;

int main()
{
     ostringstream int_to_str;
     string temp;
     int a = 0;

     cout << "Enter a number fool: ";
     cin >> a;

     int_to_str << a;
     temp = int_to_str.str();

     cout << "String " << temp << " == int " << a;

     return 0;
}

vidmaa, this doesn't work with Visual.


Clinton, it does not work in my situation. I am sure it is going to work if I do it as console application. However it does not work with the Forms :(

I looked around a bit, what I found out is that we actually need to convert an int to System::String. Who can help me with that?

Mixing C++/CLI and standard C++ can be troublesome. Try sticking to one or the other unless you have good reason not to:

label2->Text = Convert::ToString ( a );

Narue, thanks a lot! It worked finally :)

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.