Function optional arguments

otherside

Centurion
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
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 ?
 
you mean something like this ? :
Visual Basic:
    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
 
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.
 
oh you mean this then :
Visual Basic:
    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
 
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.
 
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
 
Rather than copy all 70 lines of code you could do something like

Visual Basic:
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 ;)
 
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
 
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.
 
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.
 
Back
Top