DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   string to int (http://www.daniweb.com/forums/thread122988.html)

Nemoticchigga May 7th, 2008 11:55 am
string to int
 
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.

William Hemsworth May 7th, 2008 12:00 pm
Re: string to int
 
try
Int32::Parse(stringname)

Radical Edward May 7th, 2008 12:00 pm
Re: string to int
 
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';
}

niek_e May 7th, 2008 12:01 pm
Re: string to int
 
[edit] misread the OP

Nemoticchigga May 7th, 2008 12:06 pm
Re: string to int
 
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)

Radical Edward May 7th, 2008 12:16 pm
Re: string to int
 
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);

Nemoticchigga May 7th, 2008 12:17 pm
Re: string to int
 
Great, I dont have much experience with visual studio forms, thanks a lot for your help.


All times are GMT -4. The time now is 11:41 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC