如何找到数组的维数? [英] How to find the number of dimensions that an array has?

查看:158
本文介绍了如何找到数组的维数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一段代码,在这里我需要通过传递的消息来存储有关警告消息的一些信息.传递的参数本身是一个变体,可通过对SAPListOfMessages的API调用来设置,该变体返回String的数组.但是,我注意到的是,每当出现多于1条警告时,列表都是2D,并且messageList(x-1)显然会导致错误,因为它不是正确的索引.另一个奇怪的是,for each循环似乎忽略了维数,并以某种方式将数组弄平并遍历整个数组,就好像它是一维的一样.我看到的解决此问题的唯一方法是在执行其他任何操作之前检查数组有多少维,因此是我的问题.我找不到有关获取尺寸数量的任何信息-我只找到有关其边界的信息.是否可以在VBA中找到数组的维数?如果没有,您如何建议我解决这个问题?

Below is a piece of code where I need to store some info about a warning message by going through messages passed. The parameter passed itself is a variant which is set by an API call to SAPListOfMessages which returns an array of String. What I've noticed however is that whenever there is more than 1 warning, the list is 2D and messageList(x-1) obviously leads to an error because it's not a proper index. What's also strange is that the for each loop seems to ignore dimensions and somehow just flatten the array and loop through it as if it were 1D. The only way around this I see is checking how many dimensions the array has before doing anything else and hence my question. I wasn't able to find any info on getting the number of dimensions - I only found info about their bounds. Is it possible to find the number of dimensions of an array in VBA? If not, how would you suggest I tackle this problem?

Sub getOverlapWarnings(ByRef messageList As Variant, ByRef warnings As Dictionary)

  Dim msg As Variant
  Dim x As Integer
  x = 1
 'look for an overlap warning message in the list of messages
  For Each msg In messageList
    'look for the keyword 'overlap' in the list of messages
    
    If InStr(1, msg, "overlap") <> 0 Then
       warnings.Add messageList(x - 1), msg
    End If
   x = x + 1
  Next msg
End Sub

推荐答案

是否可以在VBA中找到数组的维数?

这种方法会增加可能的尺寸计数,即60个是内置的最大值(参见注释):

This approach increments the possible dimensions count, 60 being the built in maximum (c.f. comment):

Private Function nDim(ByVal vArray As Variant) As Long
' Purpose: get array dimension (MS)
Dim dimnum     As Long
Dim ErrorCheck As Long    ' OP: As Variant
On Error GoTo FinalDimension

For dimnum = 1 To 60        ' 60 being the absolute dimensions limitation 
    ErrorCheck = LBound(vArray, dimnum)
Next
' It's good use to formally exit a procedure before error handling
' (though theoretically this wouldn't needed in this special case - see comment) 
Exit Function

FinalDimension:
nDim = dimnum - 1

End Function

其他链接(thx @ChrisNeilson)

使用数组的MS

大数组

这篇关于如何找到数组的维数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆