Groups | Blog | Home
all groups > dotnet ado.net > june 2007 >

dotnet ado.net : How can I make Console::ReadLine accepts int? In other words.


Lit
6/29/2007 3:58:10 PM
Can you do this.

int i = (int)Console::Readline();

or

string* i = Console::ReadLine();
int x;
x = (int)i;

Lit


[quoted text, click to view]

Allen Maki
6/29/2007 9:31:39 PM
Hi everybody,

I need your help.

I am using Visual C++ .NET

How can I make Console::ReadLine accepts int? In other words.

I know I can do this:

string* i = Console::ReadLine();

Can anybody tell me how can I do this:

int i = Console::Readline();

Milosz Skalecki [MCAD]
6/30/2007 12:20:01 PM
Lit,

You can't cast from string to int ;-) You have to convert it using
Convert::ToInt32(),
Int32::Parse(), Int32::TryParse() static methods.

System::String^ str = Console::ReadLine();

int x = Convert::ToInt32(str);
// or
int y = Int32::Parse(str);
// or

int z;
if (Int32::TryParse(str, z))
{
// ok you can use it
}
else
{
Console::WriteLine(
String::Format(L"'{0}' is not a valid number", str));
}

hope this helps
--
Milosz


[quoted text, click to view]
Miha Markic
6/30/2007 1:10:23 PM
You have to Convert.ToInt32 or use Int32.TryParse to convert text to int.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

[quoted text, click to view]
AddThis Social Bookmark Button