Does anyone know how to convert a "String^" data type in a visual studio 2005 forms app to an "int"? Ive tried atoi and casting to no success. Any ideas? Thanks.

Recommended Answers

All 6 Replies

C++/CLI handles boxing and unboxing implicitly. Either of these work just peachy:

using namespace System;

#include <iostream>

int main()
{
  String^ s = "123";
  int x = Convert::ToInt32(s);

  std::cout << x * x << '\n';
}
using namespace System;

#include <iostream>

int main()
{
  String^ s = "123";
  int x = Int32::Parse(s);

  std::cout << x * x << '\n';
}

[edit] misread the OP

Thanks, but new issue.

String^    comPort  = "COM" + this->RS232CommPortNum->Value;
int        baudRate = Convert::ToInt32(this->cbBaudRate->ValueMember);
SerialPort com      = gcnew SerialPort(comPort, baudRate);

error C2664: 'System::IO::Ports::SerialPort::SerialPort(System::ComponentModel::IContainer ^)' : cannot convert parameter 1 from 'System::IO::Ports::SerialPort *' to 'System::ComponentModel::IContainer ^'

this should work but it is giving me a wierd error. There are seven different overloads for SerialPort() and I dont understand why it is interpreting it this way. Any suggestions? Thanks again.

I want to use the SerialPort(SYSTEM string^ portname, int baudrate)

gcnew creates a new managed reference object. You need to declare the variable holding the object as a managed reference:

SerialPort^ com = gcnew SerialPort(comPort, baudRate);

Great, I dont have much experience with visual studio forms, thanks a lot for your help.

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.