otherside Posted July 4, 2003 Posted July 4, 2003 Hey guys, i'm sure there is some way but yet i can't find it :) how can you make an argument in a function optional example: function doit(byval as string) i want to be able to call it both as doit() and doit("one") any ideas except overriding ? Quote
Leaders dynamic_sysop Posted July 4, 2003 Leaders Posted July 4, 2003 you mean something like this ? : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strA As String = "testing" Dim strB As String = "123" DoIt(strA, strB) MessageBox.Show("The first item returned is: " & strA) MessageBox.Show("The second item returned is: " & strB) End Sub Private Function DoIt(ByVal strString As String, ByVal strSecond As String) As Array Try DoIt.SetValue(strString, 0) DoIt.SetValue(strSecond, 1) Return DoIt Catch ex As Exception End Try End Function Quote
otherside Posted July 4, 2003 Author Posted July 4, 2003 dynam thank for replying no that's not what i ment what i need is to make an argument on a function to be optional, not mandatory, so i can call the function with arguments or without arguments. to give you an example different than the last one when you create a messagebox you set some arguments to the fucntion show messagebox.show("mgs","caption",buttons) etc.. but you can make also like messagebox.show("msg") all the other arguments except the message are optional, how i do that, without copying the same function and overriding. Quote
Leaders dynamic_sysop Posted July 4, 2003 Leaders Posted July 4, 2003 oh you mean this then : Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim strA As String = "i must be used!" Dim strB As String = "i'm optional" '/// can be used , or can be left out Dim strC As String = "i'm optional also!" '/// can be used , or can be left out DoIt(strA, strB, strC) MessageBox.Show("The Default item is: " & strA) MessageBox.Show("The first Optional item is: " & strB) MessageBox.Show("The Second Optional item is: " & strC) End Sub Private Function DoIt(ByVal strDefault As String, Optional ByVal strString As String = "", Optional ByVal strSecond As String = "") As String Try Return DoIt Catch ex As Exception MessageBox.Show(ex.Message) End Try End Function Quote
otherside Posted July 4, 2003 Author Posted July 4, 2003 that's the stuff :) thanks dynam, the f..n "optional" word i was looking for , it's so sily. thanks again Quote
Administrators PlausiblyDamp Posted July 4, 2003 Administrators Posted July 4, 2003 Any reason why you want to avoid overloading in this case? Optional arguments can have odd side-effects when mixed with other .Net languages or used in DLLs. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
otherside Posted July 5, 2003 Author Posted July 5, 2003 yeah i know, but there are two reasons i needed that , fisrt on the the function i need an optional i very long over 70 lines of code and repeating that is not the best, and the operations between the different arguments are minor. The other is that some other function are very simple and doing something very simple, which is it not worth it to make overloading thanks guys Quote
Administrators PlausiblyDamp Posted July 5, 2003 Administrators Posted July 5, 2003 Rather than copy all 70 lines of code you could do something like Private Function DoIt(ByVal strDefault As String, ByVal strString As String , ByVal strSecond As String) As String 'Whatever code for the function goes here End Function Private Function DoIt(ByVal strDefault As String, ByVal strString As String) As String DoIt(strDefault,strString,"") end function Private Function DoIt(ByVal strDefault As String) As String DoIt(strDefault,"","") End Function (nicking dynamic_sysop's naming convention there ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
otherside Posted July 5, 2003 Author Posted July 5, 2003 that's a very good approach i've thought of something similar, have you experiebced and problems by using the optional parameter in dlls ? at the moment in my function works fine, but it's a software that's going to be sold so i can't risk bugs. do you beleive using the optionall parameter can cause in speciic cases bugs ? thanks plau Quote
*Gurus* divil Posted July 5, 2003 *Gurus* Posted July 5, 2003 I wouldn't want to use optional parameters in a component that's going to be sold commercially - method overloading is the OO way to go. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Administrators PlausiblyDamp Posted July 5, 2003 Administrators Posted July 5, 2003 Problem with optional parameters is they're no supported by all .Net languages. What happens is at compile time the default parameters are compiled into the calling application - thus if the defaults change in the DLL all cliens need to be recompiled. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.