使用string数组填充锯齿形数组时,“不能分配给数组错误”

我想有一个有两层的锯齿形数组:1)是一个数字,2)是一个string数组,所以我可以很容易地引用基于索引值的数组。 这样做的关键是获取文本文件的内容,以便锯齿状数组中的每个索引都是文本文件的一行(第1层=索引,第2层=行)。 这些行当然是由string填充的。

没有任何调整,文本文件在每行的string之间都有不需要的空间。 我想要string数组不包含任何浪费的空间( "Hello how are you " – > ["hello","how","are","you"] )。

我通过Trimfunction和Splitfunction来做到这一点。 Trim删除除了分隔空间以外的所有内容; Split生成我想要的那一行的string数组。 我的问题是获取数组到Jagged数组以及创build一个数组而不知道它的长度提前,因为我还没有Split文本行。

以下是我的代码。 当我使用一个变体而不是一个string作为我的第二层时,我得到了另一个我似乎无法解决的错误。 注意:包含文本文件信息的string数组是 tempString()

*更新代码:


所以你可以testing这个,使用tempString = (" test tempstring ", "", " test test test", " "," test ", "")

 Private Sub createGCStruct(ByRef tempString() As String) ' note many parameters are not included. ' also, this would be a function producing a structure, but for now I just need this to properly create a ' jagged array. ' set variables Dim tempString2() As String ReDim tempString2(UBound(tempString())) Dim j As Integer Dim jaggArray() As Variant '*****outer layer of the jagged array***** ReDim jaggArray(UBound(tempString()) + 1) Dim stringST() As String '*****inner layer of the jagged array***** Dim tempString4() As String ' set initial values of structure ' ...more code... ' capture structure information from textfile array ' A) remove unnecessary spaces from existing Array For j = LBound(tempString()) To UBound(tempString()) ' check to see if line is zero length string If tempString(j) = "" Then ' what you don't see are my commented out, futile attempts at ' solving this problem; just know that they exist Erase stringST stringST = tempString(j) jaggArray(j + 1) = stringST ' trim excesive spacing Else tempString2(j) = Trim(tempString(j)) Erase stringST stringST = Split(tempString2(j), " ") jaggArray(j + 1) = stringST End If Next j 'Below is me testing to see if this works' tempString4 = jaggArray(1) MsgBox tempString4(0), vbExclamation, "test" ' B) Add sections from array to structure '... more code... End Sub 

我可以看到“不能分配数组”错误发生在你的代码的一部分,你试图处理Split函数的奇怪的bugfeature行为,从而拆分一个空stringSplit("")返回一个string(0至 – 1)完全无法使用的数组。

“无法分配给数组”错误是由以下行引起的:

 stringST = tempString(j) 

左边的东西, stringST ,是一个数组。 右边的东西, tempString(j) ,不是。 (它是tempString数组中的一个元素 。)您不能将一个非数组赋给一个数组,因此会出错。

你可以做的是定义一个包含单个元素的数组,一个空string:

 Dim emptyStringArrayPlaceholder() As String ReDim emptyStringArrayPlaceholder(0 To 0) 

然后用它作为空string的占位符:

 stringST = emptyStringArrayPlaceholder 

以下是我如何清理你的代码:

 Private Sub createGCStruct(ByRef tempString() As String) Dim jaggArray() As Variant '*****outer layer of the jagged array***** jaggArray = splitStringArrayElements(tempString) 'Below is me testing to see if this works' tempString4 = jaggArray(1) MsgBox tempString4(0), vbExclamation, "test" ' B) Add sections from array to structure '... more code... End Sub 

我在哪里使用这个function:

 Private Function splitStringArrayElements(tempString() As String) As Variant() Dim j As Long Dim trimmedString As String Dim jaggArray() As Variant ReDim jaggArray(LBound(tempString()) To UBound(tempString())) Dim emptyStringArrayPlaceholder() As String ReDim emptyStringArrayPlaceholder(0 To 0) For j = LBound(tempString()) To UBound(tempString()) trimmedString = Trim(tempString(j)) If trimmedString = "" Then jaggArray(j) = emptyStringArrayPlaceholder Else jaggArray(j) = Split(trimmedString, " ") End If Next j splitStringElements = jaggArray End Function