Amessihel
Amessihel

Reputation: 6394

Elegant way to pass as an optional parameter to make the subroutine work as if it was omitted?

In VB6, the function Mid(string, start, [length]) has an optional parameter length. If omitted, the whole characters after the start bound will be passed.

Say I want this default behaviour only in a certain condition:

s = Mid(s, i, IIf(condition, j, TheValue)) ' What could be TheValue?

Since length is of Variant type, I tried Empty. It didn't work. Neither did -1 and Nothing. I didn't want to duplicate to Mid call in an If-Then-Else clause or somehow else. Is this possible?

Upvotes: 3

Views: 391

Answers (5)

wqw
wqw

Reputation: 11991

Here is a working sample with OP's s = Mid(s, i, IIf(condition, j, TheValue)) line

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

Notice how TheValue returns a "missing" Variant i.e. one which tests positive for IsMissing and can be used in place of optional parameters instead of not passing actual argument.

Upvotes: 2

&#201;tienne Laneville
&#201;tienne Laneville

Reputation: 5031

You could define your own Mid function:

Public Function Mid(p_sString As String, p_iStart As Integer, Optional p_iLength As Integer = -1) As String
    If p_iLength < 0 Then
        Mid = VBA.Mid(p_sString, p_iStart)
    Else
        Mid = VBA.Mid(p_sString, p_iStart, p_iLength)
    End If
End Function

This should work with the code from your question, using -1 (or any negative integer) as TheValue.

Upvotes: 2

Hel O&#39;Ween
Hel O&#39;Ween

Reputation: 1486

As an alternative to @Étienne's solution, VB provides the IsMissing method:

Public Function Mid(p_sString As String, p_iStart As Integer, Optional p_iLength As Integer) As String
    If IsMissing(p_iLength) Then
        Mid = VBA.Mid(p_sString, p_iStart)
    Else
        Mid = VBA.Mid(p_sString, p_iStart, p_iLength)
    End If
End Function

And as this wrapper method returns a string, I suggest using the String verions of Mid, which is Mid$. The later is slightly faster than the Variant version (Mid)

This was nicely explained at this site, but at the time of this posting, the request times out. Not sure if gone forever or just a temporary problem.

Upvotes: 2

Gem Taylor
Gem Taylor

Reputation: 5613

In c++, std::string these optional arguments are represented by either 0 when the default effect is zero position or length or std::string::npos when it is "infinite" length. You can explicitly supply that value and get the same behaviour.

I don't know what the equivalent constant is in m/s strings [In fact it is a different function definition, so there isn't one]. The alternative would be to pass in the string length, as that is the longest length currently possible.

The ?: ternary operator is an easy way to present 2 values with a condition to choose between them.

Upvotes: 0

Jim Mack
Jim Mack

Reputation: 1144

No such value exists. When you omit the length parameter, the compiler chooses a different path through the VBRT -- it produces different code. If you want to emulate that, you need to do the same thing, using an If-Else or similar construct to handle the two cases, like @ÉtienneLaneville suggests

Upvotes: 2

Related Questions