Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ?

  • Leaders
Posted

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

Posted

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.

  • Leaders
Posted

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

Posted

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

  • Administrators
Posted

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 ;)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...