简体   繁体   English

将字符串转换为Int32问题

[英]Converting String to Int32 Trouble

I'm having trouble converting an string to Int32 when calling it. 我在调用string时难以将其转换为Int32 This is what I'm struggling with. 这就是我在努力的目标。

public static int GetProgramLength()
{
    Console.WriteLine("Please enter program length:");
    return Int32.TryParse(Console.ReadLine);
}

Its telling me, 它告诉我,

No overload for method, "Try Parse" takes 1 argument. 方法没有重载,“ Try Parse”带有1个参数。

You need to specify the out value to use Int32.TryParse() the result of TryParse is stored to the out parameter( outParam will be 0 if it failed to convert the input string),and also it will return a boolean value to indicate whether the conversion is success or a failure; 您需要指定out值以使用Int32.TryParse() ,将TryParse的结果存储到out参数(如果无法转换输入字符串,则outParam将为0 ),并且还将返回一个布尔值以指示是否转换是成功还是失败;

So you have to correct your code as like the following: 因此,您必须像以下那样更正您的代码:

int outParam=0;
Console.WriteLine("Please enter program length:");
Int32.TryParse(Console.ReadLine(),out outParam);
return outParam;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM