473,406 Members | 2,894 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,406 software developers and data experts.

optional parameter in function definitions?

I'll ask, how do you do it?

--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
Aug 31 '07 #1
9 3867
public void MyMethod(params object[] myParams)
{
}

Then just iterate through params. If you know all the arguments are going to
be strings, or integers, or whatever, you can replace object with whatever
the type is. Otherwise just go with object[] and iterate through...
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:7A**********************************@microsof t.com...
I'll ask, how do you do it?

--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes

Aug 31 '07 #2
WebBuilder451 wrote:
I'll ask, how do you do it?
Overload the function.

Chris.
Aug 31 '07 #3
Chris Shepherd wrote:
WebBuilder451 wrote:
>I'll ask, how do you do it?

Overload the function.
For example:

void MyMethod(int required, int optional)
{
}

void MyMethod(int required)
{
MyMethod(required, 0 /* put correct default value here */);
}

I would say that the reply from "pedrito" is more suitable in what one
would consider a "variable parameter list". That is, it's not so much
that a given parameter is optional as it is that you don't even know in
advance what the parameters would be (eg similar to the CRT printf()
function).

I would stick to overloading rather than the more complicated variable
parameter list unless you really need the general-purpose behavior that
the "params object[]" technique provides.

Pete
Aug 31 '07 #4
"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
WebBuilder451 wrote:
>I'll ask, how do you do it?

Overload the function.

Chris.
Ah, that too... I was thinking in terms of an indeterminate number of
parameters....

Aug 31 '07 #5

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Chris Shepherd wrote:
>WebBuilder451 wrote:
>>I'll ask, how do you do it?

Overload the function.

For example:

void MyMethod(int required, int optional)
{
}

void MyMethod(int required)
{
MyMethod(required, 0 /* put correct default value here */);
}

I would say that the reply from "pedrito" is more suitable in what one
would consider a "variable parameter list". That is, it's not so much
that a given parameter is optional as it is that you don't even know in
advance what the parameters would be (eg similar to the CRT printf()
function).
Exacto!
I would stick to overloading rather than the more complicated variable
parameter list unless you really need the general-purpose behavior that
the "params object[]" technique provides.
Yes!
Aug 31 '07 #6
pedrito wrote:
"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
>WebBuilder451 wrote:
>>I'll ask, how do you do it?
Overload the function.

Chris.

Ah, that too... I was thinking in terms of an indeterminate number of
parameters....
I hadn't considered that. To me optional was a VB keyword that you could
use in such cases (since it didn't support overloading), so I just
assumed that was what the OP was after.

Good call on covering all the bases though! :)

Chris.
Aug 31 '07 #7

I would not do this method of an array of objects.

Too much boxing and unboxing.

Look at this post as well.

http://groups.google.com/group/micro...9d62db3?hl=en&

"pedrito" <pixbypedrito at yahoo.comwrote in message
news:y-******************************@giganews.com...
public void MyMethod(params object[] myParams)
{
}

Then just iterate through params. If you know all the arguments are going
to be strings, or integers, or whatever, you can replace object with
whatever the type is. Otherwise just go with object[] and iterate
through...
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:7A**********************************@microsof t.com...
>I'll ask, how do you do it?

--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


Aug 31 '07 #8
"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:Ov**************@TK2MSFTNGP05.phx.gbl...
pedrito wrote:
>"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
>>WebBuilder451 wrote:
I'll ask, how do you do it?
Overload the function.

Chris.

Ah, that too... I was thinking in terms of an indeterminate number of
parameters....

I hadn't considered that. To me optional was a VB keyword that you could
use in such cases (since it didn't support overloading), so I just assumed
that was what the OP was after.

Good call on covering all the bases though! :)

Chris.
Well, C++ has optional parameters as well. That is, you can assign default
values to parameters and if they're not passed, then they'll get the
default. I kind of miss it in C# because you end up creating various
overrides that all just end up calling the method with the most parameters
and assigning defaults. It'd be nice (and cleaner in the code) if the
compiled just did it for you, I think.
Sep 1 '07 #9
<"pedrito" <pixbypedrito at yahoo.com>wrote:
Well, C++ has optional parameters as well. That is, you can assign default
values to parameters and if they're not passed, then they'll get the
default. I kind of miss it in C# because you end up creating various
overrides that all just end up calling the method with the most parameters
and assigning defaults. It'd be nice (and cleaner in the code) if the
compiled just did it for you, I think.
One alternative if you've got lots of parameters is to encapsulate them
as properties in a class. This is made particularly simple with C# 3's
object initalizer syntax. You can do:

CallSomeMethod (new SomeMethodParameters { First=10, Third=20 })

(that will leave Second as its default value, assuming parameters of
First, Second and Third).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JT | last post by:
how can i declare a function that will accept an optional parameter? something like: function newFunc(strValue1, strValue2) --where strValue2 is optional. thanks much.
4
by: MLH | last post by:
The following function declaration confuses me... Public Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean I tried pasting it and its code into an...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
1
by: Do | last post by:
Hi: What's the syntax for an optional parameter in my function? I've done so many ASP 3.0 Web Classes that I haven't experienced the "Optional" parameter. Eg. Public function adduser(ByVal...
10
by: John Morgan | last post by:
Does anyone know what parameter should be used instead of Date = 0 for the optional parameter in the following function? Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date...
10
by: deko | last post by:
In VB, I could do this: MyFuncrion(this As String, that As Integer, Optional otherThing As Boolean) do stuff here End Function In C#, I can use "out" to return multiple values from a method,...
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...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.