Reputation: 28059
I am trying to recreate the following C# code in VB6:
private void ChangeTab(string tabName, bool clearAll = true)
{
Yadyyada(tabName);
if (clearAll)
{
DoMoreStuff();
}
}
Here is what I have so far:
Private Sub ChangeTab(ByVal tabName As String, Optional ByVal clearAll As Boolean)
Yadyyada(tabName)
If clearAll = True Then
DoMoreStuff
End If
End Sub
So far so good apart from the default parameter. Can I assign clearAll
a default value of true in the method signature in the same way I can in C# or do I just need to do this at the start of the method?
Thanks
Upvotes: 1
Views: 2511
Reputation: 86
You can use IsMissing Function like this
Private Sub ChangeTab(ByVal tabName As String, Optional ByVal clearAll As Boolean)
Yadyyada(tabName)
If IsMissing(clearAll) = True Or clearAll = True Then
DoMoreStuff
End If
End Sub
My Mistake! Setting a default true value for optional parameter and check for this in code is the best solution!
Private Sub ChangeTab(ByVal tabName As String, Optional ByVal clearAll As Boolean = True)
Yadyyada(tabName)
If clearAll = True Then
DoMoreStuff
End If
End Sub
Upvotes: 0
Reputation: 216243
Yes, you could do the same thing as in C#
Private Sub ChangeTab(ByVal tabName As String, Optional ByVal clearAll As Boolean = True)
Debug.Print "Value for clearAll=" & clearAll
End Sub
calling with
ChangeTab("AName")
will print True
Upvotes: 4
Reputation: 14502
Try:
Private Sub ChangeTab(ByVal tabName As String, Optional clearAll As Boolean = True)
Call Yadyyada(tabName)
If clearAll Then
DoMoreStuff
End If
End Sub
See http://msdn.microsoft.com/en-us/library/aa266305%28v=vs.60%29.aspx
Upvotes: 2
Reputation: 65049
Wow this takes me back.. can I ask why you're converting backwards technology-wise?
Anyway, you can use the Optional keyword:
Private Sub ChangeTab(ByVal tabName As String, Optional ByVal clearAll As Boolean = True)
Your issue is using ByVal. From memory, everything in VB6 was ByVal unless explicitly stated.
EDIT: I'm wrong. Default was ByRef.. it's been so long!
Upvotes: 9