Home All Groups Group Topic Archive Search About

Optional Paramter Question

Author
14 Sep 2006 4:16 AM
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.

Author
14 Sep 2006 4:23 AM
Steven Nagy
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.
Author
14 Sep 2006 2:35 PM
David Anton
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#


Show quoteHide quote
"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.
>
>
Author
14 Sep 2006 2:47 PM
David Anton
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#


Show quoteHide quote
"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.
> >
> >
Author
14 Sep 2006 6:39 AM
Tom Shelton
Miro wrote:
Show quoteHide quote
> 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
Author
14 Sep 2006 1:47 PM
Miro
Thank you Steve and Tom,

Overloading does make more sense I will follow that approach.

Thanks again,

Miro


Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1158215955.051032.307370@h48g2000cwc.googlegroups.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
>