473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Optional Parameters

I take it Optional Parameters for functions are not supported in
C#.NET. Is there a workaround of some type?
May 5 '07 #1
6 6398

"Joe Cool" <jo*****@home.netskrev i en meddelelse
news:3u********************************@4ax.com...
I take it Optional Parameters for functions are not supported in
C#.NET. Is there a workaround of some type?

May 6 '07 #2

"Joe Cool" <jo*****@home.netskrev i en meddelelse
news:3u********************************@4ax.com...
I take it Optional Parameters for functions are not supported in
C#.NET. Is there a workaround of some type?
Keyword "params".

.....Method( params object[] list ){
....}
Method( "hola",1,new object());
Best regards,
Michael Weber
May 6 '07 #3

"Joe Cool" <jo*****@home.netwrote in message
news:3u********************************@4ax.com...
>I take it Optional Parameters for functions are not supported in
C#.NET. Is there a workaround of some type?
It's called Method Overloading.

http://www.codersource.net/csharp_me...roperties.html

You have Method Overloading in VB.Net as well.

May 6 '07 #4
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

Joe Cool wrote:
I take it Optional Parameters for functions are not supported in
C#.NET. Is there a workaround of some type?
May 6 '07 #5
Bob Grommes wrote:
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.
Good advice.

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

Alun Harford
May 6 '07 #6
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

Alun Harford wrote:
Bob Grommes wrote:
>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.

Good advice.

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

Alun Harford
May 6 '07 #7

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

Similar topics

5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
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. ...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
12
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without...
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...
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: Sam Shrefler | last post by:
I'm working on creating a WebService / WebMethod to receive a record in real time from another system. The record contains about 20 fields. 10 of which aren't required. I was planning on just...
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...
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: 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
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.