Arrays 将字符串转换为整数数组

Arrays 将字符串转换为整数数组,arrays,vb.net,Arrays,Vb.net,我想将字符串转换为整数数组 Dim yValues() As Integer = strTosplit.Split(New Char() {","}).Select(Function(n) Integer.Parse(n)).Sum() 前面的代码给出了一个错误:integer类型的值无法转换为一维整数。如果需要整数数组而不是整数和,则需要数组而不是和 Dim strTosplit As String = "1,2,3" Dim yValues() As Integer = strTospl

我想将字符串转换为整数数组

 Dim yValues() As Integer = strTosplit.Split(New Char() {","}).Select(Function(n) Integer.Parse(n)).Sum()

前面的代码给出了一个错误:integer类型的值无法转换为一维整数。

如果需要整数数组而不是整数和,则需要数组而不是和

Dim strTosplit As String = "1,2,3"
Dim yValues() As Integer = strTosplit.Split({","c}).Select(Function(n) Integer.Parse(n)).ToArray

请注意,如果任何元素无法解析为整数,Integer.Parse将引发异常。

请提供代码的输入列表。strospilt()的字符串={200,700,5000}我得到一个异常:输入字符串的格式不正确。使用我发布的代码,我没有得到这样的异常。我从您在原始帖子上的评论中看到,您已将strTosplit声明为字符串数组。您可能应该将其声明为整数数组
Dim stroneedtosplit as Integer={2007005000}
。Dim cmdstring as String=“从货物中选择数量”command=New MySqlCommand(cmdstring,connection)Dim reader as MySqlDataReader=command.ExecuteReader(CommandBehavior.CloseConnection)而reader.Read()strQtyToSplit+=reader(“数量”)&“结束”,而MsgBox(strQtyToSplit)Dim yValues()作为整数=(strQtyToSplit.Split({“c}))。选择(函数(n)Integer.Parse(n))。ToArray Dim strVal作为字符串=“10”如果您正在从数据库中读取数字,则可以将其作为数字读取,或者如果必须将其作为字符串读取,则在读取时将每个数字转换为整数。我建议将数字添加到(整数)列表中。如果确实需要数组,则可以在完成读取后使用ToArray方法将列表转换为数组。