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):
Now consider the 'Add' method. For the VBA Collection object, the various permissible syntax are
but not
even though the following seems to work
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
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