473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Optional Paramter Question

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
5 1393
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gene Ariani | last post by:
Is there an elegant way to have an optional parameter in sql string like Select * from Name where (mytextbox is empty select everyone else select lastname=mytextbox) Any direction is...
5
by: iwdu15 | last post by:
hi, im rewriting my printing class i wrote and i want to make one of the parameters optional, but i cant figure out how to do it. i want it to b something like this Public Sub PrintText(Byval...
7
by: CK | last post by:
I want the procedure to check for the existence of a paramter and if it is there, it will process these instructions, otherwise it will process these instructions. Any ideas? Thanks for your...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
4
by: Mark Baldwin | last post by:
I have a web page with various controls that supply parameters to an ObjectDataSource. The ObjectDataSource has various methods that allow for filtering of the results...GetProductsByID(),...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
by: peridian | last post by:
This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work. Coming from a C++ background, having...
1
by: sugee | last post by:
hi, I need to know the equivalent of optional paramter in c#...?i know that it can be implemented using function overloading..but i need to have only one function which is an important...
3
by: araman | last post by:
i have a simple paramter query. one of the fields is called vendors. the criteria is "Like is there a way to construct the query in which "like " but if it is left blank then all record are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.