Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
VB.NET
VB.NET General Discussion
Passing optional parameters to nested functions
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Shankar_R" data-source="post: 157581" data-attributes="member: 45553"><p>Hi,</p><p></p><p>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.</p><p></p><p>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):</p><p>[XCODE=vb]Class MyGenericColClass</p><p> Protected MyCol As New Collection</p><p> Public Sub Clear()</p><p> MyCol.Clear()</p><p> End Sub</p><p> Public ReadOnly Property Count As Integer</p><p> Get</p><p> Count = MyCol.Count</p><p> End Get</p><p> End Property</p><p> </p><p> Public Sub Add(ByVal X As Object, Optional ByVal Key As String = Nothing, Optional ByVal Before As Integer = Nothing, _</p><p> Optional ByVal After As Integer = Nothing)</p><p> MsgBox(Before = Nothing)</p><p> MsgBox(After = Nothing)</p><p> MyCol.Add(X, Key, Before, After)</p><p> End Sub</p><p> End Class[/XCODE]</p><p></p><p>Now consider the 'Add' method. For the VBA Collection object, the various permissible syntax are</p><p>[XCODE=vb]Dim Col1 as new Collection</p><p>Col1.Add "Hi"</p><p>Col1.Add "Hi", "Key1"</p><p>Col1.Add "Hi", Before:=1</p><p>Col1.Add "Hi", After:=1[/XCODE]</p><p></p><p>but not</p><p>[XCODE=vb]Col1.Add "Hi", Before:=2, After:=1[/XCODE]</p><p>even though the following seems to work</p><p>[XCODE=vb]Col1.Add "Hi",Before:=Nothing, After:=Nothing[/XCODE]</p><p></p><p>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.</p><p></p><p>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.</p><p></p><p>Shankar</p></blockquote><p></p>
[QUOTE="Shankar_R, post: 157581, member: 45553"] 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): [XCODE=vb]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[/XCODE] Now consider the 'Add' method. For the VBA Collection object, the various permissible syntax are [XCODE=vb]Dim Col1 as new Collection Col1.Add "Hi" Col1.Add "Hi", "Key1" Col1.Add "Hi", Before:=1 Col1.Add "Hi", After:=1[/XCODE] but not [XCODE=vb]Col1.Add "Hi", Before:=2, After:=1[/XCODE] even though the following seems to work [XCODE=vb]Col1.Add "Hi",Before:=Nothing, After:=Nothing[/XCODE] 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 [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
VB.NET
VB.NET General Discussion
Passing optional parameters to nested functions
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom