在VB6中,该函数Mid(string, start, [length])
具有可选参数length
。如果省略,start
则将传递边界之后的整个字符。
假设我只在特定条件下才需要这种默认行为:
s = Mid(s, i, IIf(condition, j, TheValue)) ' What could be TheValue?
由于length
是Variant
类型,所以我尝试了Empty
。没用 -1
和都没有Nothing
。我不想在If-Then-Else
子句或其他某种方式中复制到Mid call 。这可能吗?
这是OPs = Mid(s, i, IIf(condition, j, TheValue))
生产线的工作示例
Option Explicit
Property Get TheValue(Optional RetVal As Variant)
TheValue = RetVal
End Property
Private Sub Form_Load()
Dim s As String
Dim i As Long
Dim j As Long
Dim condition As Boolean
s = "test test test"
i = 6: j = 3
condition = False
s = Mid(s, i, IIf(condition, j, TheValue)) '<--- this works!
Debug.Print s
End Sub
请注意,如何TheValue
返回“缺失”,Variant
即测试为肯定的IsMissing
并且可以代替可选参数而不是不传递实际参数的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句