简体   繁体   English

BC42322将'String'转换为'IFormatProvider'时可能会发生VB.NET运行时错误

[英]BC42322 VB.NET Runtime errors might occur when converting 'String' to 'IFormatProvider'

I'm trying to migrate old VB6(Visual Basic 6.0) Project to VB.net framework 4.6x. 我正在尝试将旧的VB6(Visual Basic 6.0)项目迁移到VB.net框架4.6x。 But I had to move to .net framework 2.0 first. 但是我必须首先移至.net framework 2.0。

I used to convert numbers in TextBox to formatted numbers using ToString as below. 我以前使用ToString将TextBox中的数字转换为格式化数字,如下所示。 In Visual Studio 2017 .net framework 2.0, I have BC42322 warning. 在Visual Studio 2017 .net Framework 2.0中,我有BC42322警告。 Is there a way to solve this? 有办法解决吗?

txtDividend.Text = txtDividend.Text.ToString("###,###,##0")

I also have 我也有

On Error GoTo Error_Handle

at the start of the function to handle characters in that textbox 在该函数的开头以处理该文本框中的字符

The Text property is already a string, and the ToString() method for string doesn't have the overload you want. Text属性已经是一个字符串,并且字符串的ToString()方法没有所需的重载。

Since it seems this same text field can hold both the formatted and unformatted version of a value, what you may want to do is first strip off any formatting, convert to a numeric type like Integer or Decimal , and then use the ToString() method from there: 由于似乎同一文本字段可以容纳值的格式化和未格式化版本,因此您可能要做的是先剥离所有格式,转换为IntegerDecimal类的数字类型,然后使用ToString()方法从那里:

Public Function FormatDividend(dividend As String) As String
    Dim extraCharacters As New Regex("[^\d,.-]")
    dividend = extraCharacters.Replace(dividend, "")
    Return FormatDividend(CDec(dividend))
End Function
Public Function FormatDividend(dividend As Decimal) As String
    Return Dividend.ToString("###,###,##0")
End Function

You can call those functions like this: 您可以这样调用这些函数:

txtDividend.Text = FormatDividend(txtDividend.Text)

And of course you can tweak that expression however you want, or change the overload and cast to use Integer instead of Decimal. 当然,您可以根据需要调整该表达式,也可以更改重载并强制使用Integer而不是Decimal。

Since you are trying to migrate from an environment where "1000".ToString("###,##0") produced 1,000, you will want that to work in your new (.net). 由于您正在尝试从“ 1000” .ToString(“ ###,## 0”)产生1,000的环境中进行迁移,因此您希望在新的(.net)中使用该环境。 That solution is to create an extension method that essentially provides the overload. 该解决方案是创建一个扩展方法,该方法本质上提供重载。

<Extension>
Function ToString(input as String, format as String) as String
    Static numericOnly As Regex = New Regex("[^\d.-]")
    Dim asDec as Decimal
    Decimal.TryParse(numericOnly.Replace(input, ""), asDec)
    Return asDec.ToString(format)
End Function

Since I have more than one TextBox to use format, I upgraded Joel Coehoorn 's answer to use format TextBox in my project. 由于我有多个使用TextBox的格式,因此我将Joel Coehoorn的答案升级为在项目中使用TextBox的格式。

First, I created CommonFunction.vb module file 首先,我创建了CommonFunction.vb模块文件

Imports System.Text.RegularExpressions
Module CommonFunction
    Public Function FormatNumberInText(text As String, format As String) As String
        Dim extraCharacters As New Regex("[^\d,.-]")
        text = extraCharacters.Replace(text, "")
        Return FormatNumberInText(CDec(text), format)
    End Function
    Public Function FormatNumberInText(text As Decimal, format As String) As String
        Return text.ToString(format)
    End Function
End Module

and in my TextBox vb file, I used it as below. 并在我的TextBox vb文件中按如下方式使用它。

txtDividend.Text = FormatNumberInText(txtDividend.Text, "###,###,##0")
txtDividendRatio.Text = FormatNumberInText(txtDividendRatio.Text, "###,###,##0.0#")

Thank you Joel Coehoorn . 谢谢Joel Coehoorn

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

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