Question Passing optional parameters to nested functions

Shankar_R

New member
Joined
Feb 20, 2013
Messages
2
Programming Experience
10+
Hi,

I am new to VB.NET. I have coded extensively in Excel VBA and have outgrown it, hence migrated to VB.NET. I want to implement strongly typed collection types in .NET. The recommended way is to start with Systems.Collections.CollectionBase, but I need the flexibility to get an item through both a numerical index and a key string, similar to the VBA collection.

Since the latter is available in VB.NET, but not inheritable, I thought why not make use of it and wrap it in my own collection class. So I use the following (doesn't implement all methods, but only Clear, Count and Add for the sake of illustration):
Class MyGenericColClass
        Protected MyCol As New Collection
        Public Sub Clear()
            MyCol.Clear()
        End Sub
        Public ReadOnly Property Count As Integer
            Get
                Count = MyCol.Count
            End Get
        End Property
        
        Public Sub Add(ByVal X As Object, Optional ByVal Key As String = Nothing, Optional ByVal Before As Integer = Nothing, _
                          Optional ByVal After As Integer = Nothing)
            MsgBox(Before = Nothing)
            MsgBox(After = Nothing)
            MyCol.Add(X, Key, Before, After)
        End Sub
    End Class


Now consider the 'Add' method. For the VBA Collection object, the various permissible syntax are
Dim Col1 as new Collection
Col1.Add "Hi"
Col1.Add "Hi", "Key1"
Col1.Add "Hi", Before:=1
Col1.Add "Hi", After:=1


but not
Col1.Add "Hi", Before:=2, After:=1

even though the following seems to work
Col1.Add "Hi",Before:=Nothing, After:=Nothing


But if I dimension Col1 to be MyGenericColClass then it doesn't work because even though the default values for the optional parameters Before and After are Nothing (which can be confirmed by the Msgbox's), the actual parameters being passed inside my class to the Collection object are 0 and 0. Since both Before and After values cannot simultaneously be specified, .NEt gives a runtime error.

The question is, how does the native implementation of the Collection class know that the arguments being passed are Nothing? How can I just pass these onto the Collection object in my own class so that the default values are not supplied by my wrapper? Seems like a passed-on 'Nothing' is not quite the same as the original 'Nothing'. Thanks for any insights.

Shankar
 
As an update, I found a fix- declare Before and After as Objects instead of Integer. (Key still needs to remain as String. Apparently the 'Nothing' value of Key is passed on properly. If I replace the msgbox statement by MsgBox Key=vbnullstring, I get true. However, if I call the Add method twice using vbnullstring as the key explicitly, I get an error for key clash. Doesn't happen when I use 'Nothing' as the argument though. Seems a bit counter-intuitive since I would assume that since Key is strongly typed to be a string, 'Nothing' would be reduced to vbNullString (as is confirmed by the Msgbox), but clearly something else is going on!
 

Latest posts

Back
Top