从字符串到整数类型的转换无效 [英] Conversion from string to type Integer is not valid
问题描述
我的应用程序中有以下代码......没有任何内容被声明为整数,所以我不明白或看到问题可能出在哪里......它说:
I have the below code in my app... Nothing is declared as a integer so I dont understand or see where the problem could be... It is saying:
从字符串9838 Co Rd 47"到类型整数"的转换无效.
Conversion from string "9838 Co Rd 47" to type 'Integer' is not valid.
地址作为字符串流经整个函数,我认为它可能以某种方式改变的唯一地方是在函数调用中......下面是抛出异常的函数.后面是返回地址的函数...错误来自_holder = item.bill_to.remove(address) 行
The address is flowing through the entire function as a string the only place I think it could be getting changed somehow is in a function call... Below is the function that throws the exception. Followed by the function that returns the address... The error comes from the _holder = item.bill_to.remove(address) line
For Each item In _QuickImport
Dim BusinessName As String = Nothing
Dim CustomerName As String = " "
_Id = item.id
If Not String.IsNullOrEmpty(item.Customer) Then
Dim _holder As String = String.Empty
Dim _contact_ As String = item.Contact
Dim _address_ As String = String.Empty
If item.Bill_to.Contains(_contact_) Then
_holder = item.Bill_to.Replace(_contact_, " ")
End If
If Not String.IsNullOrEmpty(_holder) Then
If item.Bill_to.Contains("Co Rd") Then
_address_ = ExtractAddressWithCoRd(_holder)
End If
Else
If item.Bill_to.Contains("Co Rd") Then
_address_ = ExtractAddressWithCoRd(item.Bill_to)
End If
End If
If Not String.IsNullOrWhiteSpace(item.Customer) Then
If item.Customer.Contains(":") Then
BusinessName = item.Customer.Split(":")(0)
CustomerName = item.Customer.Split(":")(1)
Else
CustomerName = item.Customer
End If
End If
If Not String.IsNullOrEmpty(BusinessName) Then
If item.Bill_to.Contains(BusinessName) Then
_holder = item.Bill_to.Replace(BusinessName, " ")
End If
End If
Dim _Id_ As Integer = _Id
If Not String.IsNullOrWhiteSpace(_holder) Then
Dim _check As Boolean = True
_check = ValidZip(_holder)
If _check = True Then
If Not String.IsNullOrEmpty(_address_) Then
_holder = Convert.ToString(_holder)
_address_ = Convert.ToString(_address_)
_holder = item.Bill_to.Remove(_address_)
End If
parseAddress(_holder, _Id)
另一个函数是:
Private Function ExtractAddressWithCoRd(ByVal input As String) As String
Dim add1 As String = String.Empty
Dim add2 As String = String.Empty
Dim parts() As String = input.Split(" "c)
For i As Integer = 0 To parts.Length - 1
If parts(i) = "Co" AndAlso i > 0 Then
add1 = parts(i - 1)
ElseIf parts(i) = "Rd" AndAlso i < parts.Length - 1 Then
add2 = parts(i + 1)
End If
Next
Return add1 + " Co Rd " + add2
End Function
这是我认为某些东西被更改为整数值的地方……但这也都声明为字符串……
This is where I think something is getting changed to a integer value... but this is all declared as string as well...
有什么想法吗????
推荐答案
这就是我认为某些东西变成了整数的地方价值...
This is where I think something is getting changed to a integer value...
不,不是这样.再次阅读错误信息:
No, that's not it. Read the error message again:
从字符串9838 Co Rd 47"到类型整数"的转换无效.
Conversion from string "9838 Co Rd 47" to type 'Integer' is not valid.
从字符串转换"意味着你的 _address_
变量确实是一个字符串,它的值甚至被显示出来(9838 Co Rd 47").
"Conversion from string" means that your _address_
variable really IS a string, and it's value is even displayed ("9838 Co Rd 47").
它尝试将此字符串转换为整数,因为 item.Bill_to
是一个 String
而 String.Remove
需要一个整数参数(从中删除字符的位置),但您传递的是一个字符串.您正在寻找的是 String.Replace
,您已经在别处使用了它:
It tries to convert this string to an integer because item.Bill_to
is a String
and String.Remove
expects an integer argument (the position from which to remove characters), but you pass a string instead. What you're looking for is String.Replace
, which you already use elsewhere:
_holder = item.Bill_to.Replace(_address_, "")
这篇关于从字符串到整数类型的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!