By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,711 Members | 2,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

Optional Paramter Question

P: n/a
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.


Sep 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Sep 14 '06 #2

P: n/a

Miro wrote:
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

--
Tom Shelton

Sep 14 '06 #3

P: n/a
Thank you Steve and Tom,

Overloading does make more sense I will follow that approach.

Thanks again,

Miro
"Tom Shelton" <to*@mtogden.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
Miro wrote:
>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

--
Tom Shelton

Sep 14 '06 #4

P: n/a
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#
"Steven Nagy" wrote:
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.

Sep 14 '06 #5

P: n/a
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#
"David Anton" wrote:
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#
"Steven Nagy" wrote:
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.
Sep 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.