Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, there !

 

I have a sub which have some arguments. One of the is optional. The problem is that this optional argument is a System.Windows.Form.Button...

 

If I declare the sub like this, it doesn't work :

private sub createNew(Byval text as string, byval number as integer, optional byval buttonSelected as System.Windows.Form.Button = ??????)
...
...
end sub

 

I don't know how to put instead of the ????? Someone has an idea ?

Thanks!

  • *Experts*
Posted

A better way, and a .NET way would be to make overloads.

Here is an example:

Private Overloads Sub createNew(ByVal text As String, ByVal number As Integer, ByVal buttonSelected As System.Windows.Form.Button)
       'do whatever you need to when the button is instance is passed in
End Sub

Private Overloads Sub createNew(ByVal text As String, ByVal number As Integer)
        'do whatever you need to when a button is not passed in
End Sub

Posted

Hello mutant,

 

This way is quite interesting, but my sub is very very long. And it's only a very small thing which change if we give button in argument. So I have to write 2 times all the code, and modify it 2 times if I need to (and I surely have to...;) ).

 

But well, since I or someone find the "optional" solution, i'll try with your method, thanks!

  • Administrators
Posted

Quick re-hash of mutans suggestion follows....

 

Private Overloads Sub createNew(ByVal text As String, ByVal number As Integer, ByVal buttonSelected As System.Windows.Form.Button)
       'do whatever you need to when the button is instance is passed in
End Sub

Private Overloads Sub createNew(ByVal text As String, ByVal number As Integer)
     'Call the full method but provide a default button parameter
      createNew (text, number, new button)    'Replace the last parameter with whatever the default should be
End Sub

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...