Groups | Blog | Home
all groups > c# > may 2007 >

c# : Optional Parameters


Mr. Arnold
5/5/2007 8:57:03 PM

[quoted text, click to view]

It's called Method Overloading.

http://www.codersource.net/csharp_methods_properties.html

You have Method Overloading in VB.Net as well.
Bob Grommes
5/5/2007 9:00:06 PM
Optional parameters are not quite the same as either params[] or
overloading, but overloading usually serves fine.

Where VB's optional parameters come in handy, really, is when you call
into those obscene automation APIs in Office and elsewhere ... you know,
the ones where you have twenty-five parameters and you only need to use
the first two, and furthermore, you likely will never use the other
twenty-three. You can make the same call in C#, but it requires more
effort.

This little convenience is why VB is preferred by some for automation
work, although I suspect it either increases call overhead or involves
late binding under the hood. But it's an edge case. For everyday work,
overloading does the job and arguably does it better.

--Bob

[quoted text, click to view]
Joe Cool
5/5/2007 11:49:08 PM
I take it Optional Parameters for functions are not supported in
Michael Weber
5/6/2007 2:02:44 AM

"Joe Cool" <joecool@home.net> skrev i en meddelelse
news:3u5q335dohfmgafn0bgkir1r1i2eognbrl@4ax.com...
[quoted text, click to view]

Michael Weber
5/6/2007 2:06:52 AM

"Joe Cool" <joecool@home.net> skrev i en meddelelse
news:3u5q335dohfmgafn0bgkir1r1i2eognbrl@4ax.com...
[quoted text, click to view]

Keyword "params".

.....Method( params object[] list ){
....}


Method( "hola",1,new object());


Best regards,
Michael Weber

Bob Grommes
5/6/2007 7:37:54 AM
In the specific case of an existing API like the one I described, that's
poorly designed in the first place ... no joy. But yes, any time you
have that much info to pass in, it's screaming for a class wrapper.

--Bob

[quoted text, click to view]
Alun Harford
5/6/2007 12:18:14 PM
[quoted text, click to view]

Good advice.

Another option is to go the OO way, and create an object to encapsulate
the arguments for your method.

AddThis Social Bookmark Button