【问题标题】:How to write an or statement without a duplicating code (VBA)如何在没有重复代码的情况下编写 or 语句(VBA)
【发布时间】:2020-01-08 23:43:19
【问题描述】:

我知道在大多数其他语言中,您可以编写表达式if (ptr == NULL || ptr->foo()) {something bad happened} 来测试变量的有效性和您想要执行的测试。 VBA 不允许您这样做。因此,我绞尽脑汁想出一种方法来编写以下代码,而无需 1)使用错误捕获作为条件分支的手段,以及 2)不重复代码。

Type Vector
   vData() as Variant
   vSize as Long
End Type

Sub add(v as Vector, elem as Variant)
   Dim oldSize as Long
   if v.vSize = 0 Or UBound(v.vData) >= v.vSize then
      oldSize = v.vSize
      ReDim Preserve v.vData(0 to (oldSize * 2 + 1))
      v.vData(oldSize) = elem
      v.vSize = v.vSize + 1
   else
      v.vData(v.vSize) = elem
      v.vSize = v.vSize + 1
   end if
End Sub

现在无论 vSize 是否为 0(如果 vData 从未被 Dim'd),该代码都会在 UBound 行上崩溃。我能看到的唯一另一种方法是执行额外的 elseif 语句来检查 UBound,但这会重复将向量大小加倍的代码。

如果您认为这是重复的:VBA Short-Circuit `And` Alternatives。这讨论了 AND 语句(非或)的替代方案。嵌套 ifs(又名 AND 语句)不会像 OR 那样重复代码。

【问题讨论】:

    标签: vba


    【解决方案1】:

    如果我理解正确,您需要检查数组是否已分配。

    一个这样的选择是这样做(不管它看起来多么奇怪):

    If (Not Not MyArray) <> 0 Then 'Means it is allocated
    

    答案来自this thread - 更多想法请参阅。

    【讨论】:

      【解决方案2】:

      使用 SnowGroomer 的答案,我发布了完整的解决方案:

      这是一个名为 Vector 的类

      Private data() As Variant
      Private size As Long
      
      Property Get vSize() As Long
          vSize = size
      End Property
      
      Property Let vData(ByVal index As Long, elem As Variant)
          If index < 0 Then Exit Property
          If index < size Then
              data(index) = elem
          Else
              Me.add elem, index
          End If
      End Property
      
      Property Get vData(ByVal index As Long) As Variant
          If index < 0 Or (Not Not data) = 0 Then
              vData = Nothing
              Exit Property
          End If
          vData = data(index)
      End Property
      
      Public Sub add(elem As Variant, Optional index As Long = -1)
          If index > -2 Then
              If index = -1 Then
                  If size = 0 Or (Not Not data) = 0 Then
                      ReDim data(0)
                      data(size) = elem
                      size = size + 1
                      Exit Sub
                  Else 'size <> 0
                      ReDim Preserve data(0 To size * 2 + 1)
                      data(size) = elem
                      size = size + 1
                  End If
              Else 'index <> -1
                  If index >= size Then
                      ReDim Preserve data(0 To index)
                      data(index) = elem
                      size = index + 1
                  Else 'index < vSize
                      data(index) = elem
                  End If 'index >= vSize
              End If 'index = -1
          End If 'index > -2
      End Sub 'add
      

      【讨论】:

        猜你喜欢
        • 2021-12-02
        • 2019-03-06
        • 2022-12-04
        • 2021-02-03
        • 2018-01-01
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        • 2017-02-27
        相关资源
        最近更新 更多