2016-09-14 61 views
0

这是之前回答并解决的问题的扩展。试图让数组的数组,并遇到了同样的错误“只能在公共对象模块用户自定义类型都可以强制......”VBA:使用自定义数据类型和函数返回数组(array(),array())

我期待到使用集合,但还没有想通了这一点。

Option Explicit 

Public Type aType 
    P_Col As Integer 
    P_Rad As Single 
    P_X As Single 
    P_Y As Single 
End Type 

Public Type bType 
    Shp1() As aType 
    Shp2() As aType 
    Shp3() As aType 
End Type 



Function MakePatterns() As bType() 
Dim i  As Integer 
Dim circles() As aType 
Dim triangles() As aType 
Dim squares() As aType 
Dim shps()  As bType 


For i = 1 To 5 
    ReDim Preserve circles(i) 
    circles(i).P_Col = Int(i/2) 
    circles(i).P_Rad = i 
    circles(i).P_X = i * 10 + 1 
    circles(i).P_Y = i * 10 + 5 
Next 

For i = 1 To 5 
    ReDim Preserve triangles(i) 
    triangles(i).P_Col = Int(i/2) 
    triangles(i).P_Rad = i 
    triangles(i).P_X = i * 10 + 1 
    triangles(i).P_Y = i * 10 + 5 
Next 

For i = 1 To 5 
    ReDim Preserve squares(i) 
    squares(i).P_Col = Int(i/2) 
    squares(i).P_Rad = i 
    squares(i).P_X = i * 10 + 1 
    squares(i).P_Y = i * 10 + 5 
Next 

For i = 1 To 5 
    ReDim Preserve shps(i) 
    shps(i) = Array(circles(i), triangles(i), squares(i)) 
Next 

'For i = 1 To 5 
' Debug.Print circles(i).P_Col; circles(i).P_Rad; _ 
'  circles(i).P_X; circles(i).P_Y 
'Next 

MakePatterns = shps 

End Function 
+0

如果你知道元素的数量,这是更好地使用数组和调整它们的大小只有一次。例如'Dim circles(1 to 5)As aType' – Slai

+0

感谢Slai。实际上,元素的数量并不总是相同的。也许我不应该将它简化为5。事实上,我不确定如何处理不同的尺寸,而不会在返回的数组中留下空元素。 如果圆(10),三角形(4),广场(56),我在想这样做了56次(最大UBOUND),并留下大量的空.shp1和.shp2的。否则,我正在考虑为每个循环做单独的循环。 – user110084

+0

['Scripting.Dictionary'](http://stackoverflow.com/documentation/vba/3667/scripting-dictionary-object)应该比['Collection']容易一点(http://stackoverflow.com/documentation/vba/5838/collections) – Slai

回答

0

因此引发你的异常的事情是,你使用的aType类型的数组为Shp1等。之后,你实现了它,因为它只是aType的一种类型,而不是aType的数组。

现在,基于你的回答,我想出了这个代码。您将以5个元素bType结尾,每个元素包含aType的5个元素。但是,您可以根据需要更改此选项,问题是,您现在有一个类型为bType的数组,其中包含类型为aType的数组以容纳圆,三角形和正方形。

Option Explicit 

Public Type aType 
    P_Col As Integer 
    P_Rad As Single 
    P_X As Single 
    P_Y As Single 
End Type 

Public Type bType 
    circles()  As aType 
    triangles() As aType 
    squares()  As aType 
End Type 

Function MakePatterns() As bType() 
Dim i, j  As Integer 
Dim shps()  As bType 


ReDim shps(4) 
For i = LBound(shps) To UBound(shps) 

     For j = 0 To 4 
     ReDim Preserve shps(i).circles(j) 
     With shps(i).circles(j) 
      .P_Col = Int(j/2) 
      .P_Rad = j 
      .P_X = j * 10 + 1 
      .P_Y = j * 10 + 5 
     End With 
     Next j 

     For j = 0 To 4 
     ReDim Preserve shps(i).triangles(j) 
     With shps(i).triangles(j) 
      .P_Col = Int(j/2) 
      .P_Rad = j 
      .P_X = j * 10 + 1 
      .P_Y = j * 10 + 5 
     End With 
     Next j 

     For j = 0 To 4 
     ReDim Preserve shps(i).squares(j) 
     With shps(i).squares(j) 
      .P_Col = Int(j/2) 
      .P_Rad = j 
      .P_X = j * 10 + 1 
      .P_Y = j * 10 + 5 
     End With 
     Next j 

Next i 

MakePatterns = shps 

End Function 

另外两个提示:

  • 如果你已经知道你想有5项,申报5项阵列,而不是不断地调整它。
  • 数组的索引从0开始,所以你可能想改变你的循环,以For i=0 To 4。否则,最终会得到第一个对象bType,其中仅包含aType的每个属性中的值为0的项目。
+0

谢谢你Codeguy。现在按照建议更正。 实际上,shp1,shp2和shp3最初有不同的尺寸,为了简单起见,我将它们全部设为5。如果它们不同,大概我将不得不在单独的循环中执行Shps(i).shp1,Shps(i).shp2等等,或者冒着用“0”填充较短的那个,对吧?或者是找到最大UBound和循环次数的情况? 也感谢零基地提醒,是的,我已经看到第一个元素中的“0”! – user110084

+0

没问题,乐意帮忙。 现在提供的解决方案,类型'bType'的元素总是由'aType'的三个元素组成。你想要什么'bType'元素组成?你想要它由'aType'类型的三个_arrays_元素组成吗? – codeguy

+0

bType和aType都有固定数量的元素,但不一样。我想知道的是Shp1(i)和Shp2(i)和Shp3(i),它们可能不具有相同的长度。所以shp(i)可能会说(Shp1(5),Shp2(9),Shp3(2)),并且我使用三个循环来填充shp(i).Shp1(),shp(i).Shp2()和shp(i).Shp3(),这似乎效率低下。或者,我已经设置了9个计数(最高uBound)的一个循环,但最终在Shp1和Shp3中有空的内容,这有点难看。有没有更好的方法来处理这个问题?再次感谢。 – user110084