Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ack - seems I am always in here admiting I don't know something :)

 

In any case is there a way to see if an optional paramater was included in the original call to a function or sub?

 

Thanks

  • *Experts*
Posted

You shouldn't be using Optional parameters anyway. You should

overload the sub.

 

Private Sub SomeFunction(param As String)
 msgBox param & " was passed!"
End Sub

Private Sub SomeFunction()
 MsgBox "Nothing was passed."
End Sub

  • *Experts*
Posted

To add what VolteFace said (thanks, btw, didn't know VB.NET supported overloads):

 

You can mimic your own custom optional by moving all of your "real" code into the function that takes the params. For example:

Private Sub SomeFunction()
   Console.WriteLine("Nothing passed")
   SomeFunction(String.Empty)
End Sub

Private Sub SomeFunction(param As String)
   ' Can you use + in VB.NET or is that C# only?
   ' You might need to use &
   Console.WriteLine("Passed: " + param)
End Sub

 

This is handy in a lot of cases where you want to use an optional parameter but you don't want to be VB.NET specific. In the example above, the param isn't really optional, but you CAN call it as:

SomeFunction()

or

SomeFunction("Test")

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

If I remember right, the compiled code that gets created is basically creating overloads for all of your optional arguments. You get more control with your own overloads and, I think, they're easier to use. You only have to code the overload once and hopefully you don't have TOO many optional params. If you have more than a couple of optionals you may be trying to punch too much bang for the buck into one function OR you probably end up only using 3 or 4 "versions" of the function (meaning, which params are passed and which are left "optional").

 

And, don't be surprised if VB.NET version 3 cuts out the optionals :)

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

Nerseus, in VB.NET you can use either + or & to concatenate

strings, but it is much better if you use & only for concatenation.

Plus, if you're doing a LOT of concatenating, use a StringBuilder

class, instead. :)

 

Save the + operator for addition.

 

[edit]

Also, relating back to the thread subject... I never use optional

parameters; I always overload the methods. It helps organize

things, and the IntelliSense is nicer, too. :)

[/edit]

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

Ah, works the same as VB6 then. Goooood to know. :)

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

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