VB6使用Split拆分字符串到数组报'Can't assign into array'编译错误
VB6 Split函数赋值数组报错解决方案
错误原因
- 第一次定长String数组报错:VB6中
Split函数返回值为动态长度的String类型数组,固定长度的数组不支持直接接收Split的返回结果,因此触发「Can't assign into array」编译错误。 - 第二次Variant动态数组报错:Split返回的数组基础类型为String,和声明的
Variant()类型不匹配,因此依然报错。
正确实现步骤
- 声明动态String类型数组接收Split结果,不要指定初始长度,也不要使用Variant数组类型
- 增加数组边界判断,避免原字符串无冒号时出现下标越界错误
- 可选:增加Trim处理匹配逻辑,避免字符串前后多余空格导致匹配失败
可运行代码示例
' 声明动态字符串数组,不指定固定长度 Dim deliveryType() As String ' 接收Split拆分结果 deliveryType = Split(vaGoodsInLine.FieldValue("Comment"), ":") ' 先判断数组有效再做匹配逻辑 If UBound(deliveryType) >= 0 Then If Trim(deliveryType(0)) = "By Value" Then ' 此处写入你的业务逻辑 End If End If
可选兼容方案
如果确实需要用Variant类型接收,直接声明为单个Variant而非数组类型即可:
Dim deliveryType As Variant deliveryType = Split(vaGoodsInLine.FieldValue("Comment"), ":")
该方案类型安全性不如动态String数组,仅作为兼容场景备选。
内容的提问来源于stack exchange,提问作者Jean Camargo
相关产品推荐
相关产品推荐

