Optional Paramter Question

  • Thread starter Thread starter Miro
  • Start date Start date
M

Miro

This qustion is probably for people who have created large apps with subs /
or functions that have
a lot of parameters and used in a lot of places in ur whole app. ( lets say
its ur own library function )
-Not looking for a big complicated question... ( dont waste a lot of ur time
please )

I have been searching for the "Optional" parameter forever in VB.net
I have found a pretty down to earth article on why NOT to use them....

http://www.knowdotnet.com/articles/optionalparams.html

so a simple example of doing this is:

Sub DisplayMyName(ByVal FirstName As String, Optional ByVal LastName As
String = "")
' do stuff
End Sub

So you can call this function in 1 of two ways:
DisplayMyName( "John", "" ) ' Without hte optoinal clause
or DisplayMyName( "John", "Smith" )

I havnt gotten to the point where I have created a Function or a Sub with a
bunch of parameters...
but I was wondering what do you vb.net longs do?

When you add a new paramter to the end of a Sub or a Function that would be
used everywhere in tonnes of places
in your whole application, ( lets say its like a library function ), what do
you do:
1. Go to all your Function calls and add a , "" - and dont use the
Optional Clause.
2. Or do you actually use the "Optoinal" clause - and then from then on,
any new paramter add it as an optoinal clause as well.

From the article what I am understanding is they say I should be doing #1,
but I would like to know
what 'normal' vb.net code writers do ? Once the code gets big, option1
seems like it becomes a big problem if used
in plenty of places.

Thanks,

M.
 
Perhaps overloading the method would be better instead?
Then you can customise your functionality depending on whether or not
the new parameter exists.

"optional" does not exist in C# and probably with good reason.
I'm not totally sure, but is "optional" an old VB6 hangup?

If your methods are THAT long and have huge parameter lists, I think
the real issue you need to tackle is refactoring. Just my opinion mind
you.
 
Miro said:
This qustion is probably for people who have created large apps with subs /
or functions that have
a lot of parameters and used in a lot of places in ur whole app. ( lets say
its ur own library function )
-Not looking for a big complicated question... ( dont waste a lot of ur time
please )

I have been searching for the "Optional" parameter forever in VB.net
I have found a pretty down to earth article on why NOT to use them....

http://www.knowdotnet.com/articles/optionalparams.html

so a simple example of doing this is:

Sub DisplayMyName(ByVal FirstName As String, Optional ByVal LastName As
String = "")
' do stuff
End Sub

So you can call this function in 1 of two ways:
DisplayMyName( "John", "" ) ' Without hte optoinal clause
or DisplayMyName( "John", "Smith" )

I havnt gotten to the point where I have created a Function or a Sub with a
bunch of parameters...
but I was wondering what do you vb.net longs do?

When you add a new paramter to the end of a Sub or a Function that would be
used everywhere in tonnes of places
in your whole application, ( lets say its like a library function ), what do
you do:
1. Go to all your Function calls and add a , "" - and dont use the
Optional Clause.
2. Or do you actually use the "Optoinal" clause - and then from then on,
any new paramter add it as an optoinal clause as well.

From the article what I am understanding is they say I should be doing #1,
but I would like to know
what 'normal' vb.net code writers do ? Once the code gets big, option1
seems like it becomes a big problem if used
in plenty of places.

Thanks,

M.

I totally agree with Steve. Don't use optional - you can overload your
functions/Subs in VB.NET, so it is really not needed. So, for example
I would write your above code like this:

Option Strict On
Option Explicit On

Imports System

Module Module1

Sub Main()
DisplayMyName("Tom")
DisplayMyName("Tom", "Shelton")
End Sub

Sub DisplayMyName(ByVal firstName As String)
DisplayMyName(firstName, String.Empty)
End Sub

Sub DisplayMyName(ByVal firstName As String, ByVal lastName As
String)
Console.WriteLine("{0} {1}", firstName, lastName)
End Sub
End Module

HTH
 
Optional parameters is certainly not an old classic VB hangup.
More languages allow optional parameters than those that don't (another .NET
language that has optional parameters is IronPython).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
Oops - after googling a bit on this it appears that it's roughly split down
the middle.
VB, Classic C++, Python, Ruby, and other scripting languages allow optional
parameters.
C++/CLI, C#, Java, and Delphi don't allow them.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
Back
Top