How to use system::Convert.ToInt32();

Hello, I am trying to learn how to make a simple calculator in Visual Studio 2017.
I am following the instructions of a book, and I am trying to use Convert.ToInt32.

But when I do I get this error message: "Type name is now allowed".
I have googled and I read the microsoft reference on how to use it properly.
I do exactly as instructed in the book, and I seem to use it as I intended.
I would also like to know why I have to use the ^ sign when using string^.

Thank you very much.

I included the relevant bits of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public:
	int sum;
	System::String^ newNr;
	MyForm(void)
	{
		InitializeComponent();
		sum = 0;
		newNr = "";
	}

private: void AddDigit(String ^ nr)
{
	textBox1->AppendText(nr);
	newNr += nr;
}

private: System::Void button_add_Click(System::Object^  sender, System::EventArgs^  e) {
	sum = sum + Convert.ToInt32(newNr); //Convert type name not allowed
	newNr = "";
	textBox1->Text = sum + "+";
}
Last edited on
But when I do I get this error message: "Type name is now allowed".

Why don't you post the verbatim error message? It is a lot more helpful than you doing it from memory.

I don't see an immediate problem with line 25. However, line 27 looks odd to me. You are trying to add an int to a string literal. This can't be done in normal C++, and probably not in your version of C++ either.
textBox1->Text = sum + "+";

This is not standard C++, but Microsoft's C++/CLI. I have no idea what string^ means because that's Microsoft's custom thing. I would just google "C++ CLI string caret symbol".
This is the complete error message I got, the code is E0254.

On line 27 I don't know what to say, this is exactly how its done in the book.
I tested it and it worked.

Thank you.
Last edited on
It's definitely isn't the verbatim error message, especially since you misspelled "not" as "now". Does the error message really have no more information than that? Weird...

Sorry, I don't have C++/CLI on the computer I'm currently using, so I can't make an example project to test myself.

Maybe try making a new project yourself and making it as simple as you can possible make it while still reproducing the error message. Then, post the simplified source code that someone else could compile.
Last edited on
Oh, I see. By "error" you are only referring to the IntelliSense message. IntelliSense messages do not always give full context as to what the actual problem is!

If you actually go to Build -> Build Solution you'll see more errors pop up in the Error List.

Possible solution:

Change
Convert.ToInt32(newNr);
to
Convert::ToInt32(newNr);

. is used in C# and VB for static classes.
:: must be used in C++ for static classes.

See examples at:
https://msdn.microsoft.com/en-us/library/1k20k614(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1
Last edited on
Topic archived. No new replies allowed.