Last post Feb 03, 2009 12:38 PM by Hungry4Knowledge
Feb 02, 2009 11:16 AM|planet idiot|LINK
Feb 02, 2009 01:10 PM|Hungry4Knowledge|LINK
Are you putting a default value for the optional parameters? This should allow the procedure to be called and supply it's own value if one isn't already provided. Here's how it would look in the parameter area:
Optional ByVal OptionalParam
As Boolean =
I'm under the impression that an overloaded sub/function is chosen by the parameter datatype, so if both sub/funtions have the same parameters with the same datatypes it might never choose the one you
expected. I would also believe that the datatype would have to be decided(cast) before the sub/function call to allow the correct sub/function to be chosen if there is a difference.
Feb 02, 2009 08:09 PM|David Anton|LINK
The problem with converting a VB method with optional parameters to a set of overloaded methods is that it's difficult to ensure that the calling code won't break.
Sub test(Optional ByVal i As Integer = 0, Optional ByVal b As Boolean = False, Optional ByVal s As String = "abc")
If you convert this to four overloaded methods you might think you've got the bases covered but you don't - a user of the dll might be calling it like with silly VB code like the following (VB allows bypassing arguments matching optional parameters with
test( , , "hey")
test(1, , "hey")
It gets much worse with more parameters.
Feb 03, 2009 09:45 AM|planet idiot|LINK
Feb 03, 2009 09:52 AM|David Anton|LINK
That's exactly what I was getting at.
Regardless of whether your new overload replacements are in VB or C#, you'll find that overloaded methods are only equivalent if you can go through all the code referencing the original optional parameter method and adjust the calls also.
Feb 03, 2009 12:38 PM|Hungry4Knowledge|LINK
This is a stretch but you could look at exposing your methods with property get methods. This way you could handle parameters that you know that the user would be responsible for and handle the rest in the property get code.
For example with the code that David used in his example with the Test sub.
Private Function test(Optional ByVal i As Integer = 0, Optional ByVal b As Boolean = False, Optional ByVal s As String = "abc") as String
This function could be exposed with several property get statements that would force the user into picking specific combinations of the original function where you handle how it works in the get statement itself.
Public ReadOnly Property TestInt(MyNumber) As Integer
Public ReadOnly Property TestStr(MyString) As Integer
Public ReadOnly Property TestIntStr(MyInt, MyString) As Integer
Return Test(i:=MyInt, s:=MyString)
I've read this over and I don't think that I've made it clear enough that with the Get statements you would decide what options to pass to the functions whether they be default or something different altogether. My point is that you can take control of the code that you've already written and expose it however it should be exposed to the user.