[英]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 ofTryParse
is stored to the out parameter(outParam
will be0
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.