错误:'int.TryParse(string,out int)'的最佳重载方法匹配包含一些无效参数

编程入门 行业动态 更新时间:2024-10-11 19:23:31
本文介绍了错误:'int.TryParse(string,out int)'的最佳重载方法匹配包含一些无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在if(Int32.TryParse(strNumber,out strNumb)== false)中有一个错误说'int.TryParse(string,out int)'的最佳重载方法匹配有一些无效的参数。 非常感谢任何帮助。

I have a error at "if (Int32.TryParse( strNumber, out strNumb) == false) " saying that The best overloaded method match for 'int.TryParse(string, out int)' has some invalid argument. Any help is really appreciated.

public static int CheckNullInteger(int strNumber) { //Return IIf(strNumber.Length = 0, 0, CType(strNumber, Integer)) int strNumb = 0; //Dim drvw As DataRowView = Nothing if (strNumber == null) { return 0; } if (Int32.TryParse( strNumber, out strNumb) == false) { return 0; } else { return Convert.ToInt32(strNumber); } }

推荐答案

您的代码永远不会编译: 1.如果您将参数类型指定为'int,它将不接受任何其他数字类型:您将收到编译时错误。 2. Int32.TryParse期望一个字符串参数,而不是一个整数,作为它的第一个参数。 除了两个明显的编译时问题: 3.方法的整数参数永远不会为null,因此测试'strNumber是否为null将始终返回'false。 我想知道你想要的是这样的: Your code as is will never compile: 1. if you specify a parameter type as 'int, it will not accept any other numeric Type: you'll get a compile time error. 2. Int32.TryParse expects a string argument, not an integer, as its first parameter. In addition to the two obvious compile-time issues: 3. an integer parameter to a method will never be null, so testing if 'strNumber is null will always return 'false. I wonder if what you want is something like this: public static int CheckNullInteger(string strNumber) { int strNumb; if (Int32.TryParse(strNumber, out strNumb)) return strNumb; // choose some value to indicate no conversion was done return -1; }

我自己的偏好是使用这样一个方法的可空整数返回值

My own preference would be to use a nullable integer return value from such a method

public static int? CheckNullInteger(string strNumber) { int strNumb; if (Int32.TryParse(strNumber, out strNumb)) return strNumb; // conversion failed: return null return null; }

然后,在使用转化的代码中,您可以检查转化是否成功:

Then, in the code that uses the conversion, you can check whether or not the conversion was successful:

int? convertedInt = CheckNullInteger("100.0"); if (convertedInt == null) { // handle conversion failure } else { // use value of 'convertedInt }

但是,显然,这里可能存在与文化语言相关的问题,因为可以使用其他小数点指示符字形,如逗号。如果我需要用数字语法来处理文化/语言问题,我希望返回一个整数值,如果字符串中包含任何可能的数字类型 :那我就是使用这样的技术:

But, obviously, there could be culture-language-dependent issues here since other decimal-point indicator glyphs could be used, like a comma. If I needed to deal with culture/language issues in numeric syntax, and I wanted to return an integer value if the string held any possible Type of number: then I'd use a technique like this:

// requires using System.Globalization; public static int? CheckNullInteger(string strNumber) { double convertedValue; if(! double.TryParse(strNumber, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out convertedValue)) return null; return Convert.ToInt32(convertedValue); }

如果您需要预先筛选一个字符串以确保其中的每个字符都与某种类型的数字兼容,那么Linq很有用;示例:

If you need to pre-screen a string to make sure every character in it is compatible with being some type of number, then Linq is useful; example:

// requires Linq bool isNumber = "100.3".All(chr => char.Number(chr) || chr == '.');

另一种方法是使用RegEx筛选字符串以用作数值。如果你想深入了解这些事情的各种方法(及其表现),我建议:[ ^ ]

Another alternative is to use a RegEx to screen strings for use as numeric values. If you want to get deep into the various methods (and their performance) of such things, I recommend:[^]

我有这样的改变我在Tryparse中混淆接受字符串而不是整数。现在它的工作正常。谢谢 public static int CheckNullInteger(String strNumber) { //返回IIf(strNumber.Length = 0,0,CType(strNumber,Integer)) int strNumb = 0; // Dim drvw As DataRowView = Nothing if (strNumber == null){ 返回0; } if(Int32.TryParse(strNumber) ,strNumb)== false) { 返回0; } 其他 { 返回Convert.ToInt32(strNumber); } } I have change like this i was confusing in Tryparse accepts string not the integer. now its working fine. Thanks public static int CheckNullInteger(String strNumber) { //Return IIf(strNumber.Length = 0, 0, CType(strNumber, Integer)) int strNumb = 0; //Dim drvw As DataRowView = Nothing if (strNumber == null) { return 0; } if (Int32.TryParse( strNumber, out strNumb) == false) { return 0; } else { return Convert.ToInt32(strNumber); } }

更多推荐

错误:'int.TryParse(string,out int)'的最佳重载方法匹配包含一些无效参数

本文发布于:2023-06-03 07:26:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   参数   方法   TryParse   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!