How do I test if optional arguments are supplied or not?

前端 未结 8 416
后悔当初
后悔当初 2020-12-05 09:20

How do I test if optional arguments are supplied or not? -- in VB6 / VBA

Function func (Optional ByRef arg As Variant = Nothing)

    If arg Is Nothing Then          


        
相关标签:
8条回答
  • 2020-12-05 10:00

    Use IsMissing:

    If IsMissing(arg) Then
        MsgBox "Parameter arg not passed"
    End If
    

    However, if I remember correctly, this doesn’t work when giving a default for the argument, and in any case it makes using the default argument rather redundant.

    0 讨论(0)
  • 2020-12-05 10:02

    If you are using a string or number variable you can check the value of the variable. For example:

    Function func (Optional Str as String, Optional Num as Integer)
    
    If Str = "" Then
        MsgBox "NOT SENT"
    End If
    
    If Num = 0 Then
        MsgBox "NOT SENT"
    End If
    
    End Function
    

    This allows you to use non-variant variables.

    0 讨论(0)
提交回复
热议问题