473,414 Members | 1,599 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,414 software developers and data experts.

What is the equivalent to VB optional paramaters in C#?

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, and "params"
to send in an array, but is it possible to have optional parameters? How do
I do this in C#?

Thanks in advance.

Mar 1 '06 #1
10 6929
deko <de**@nospam.com> wrote:
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, and "params"
to send in an array, but is it possible to have optional parameters? How do
I do this in C#?


You can't. The normal equivalent is to create overloads for the method.

--
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
Mar 1 '06 #2
but is it possible to have optional parameters? How do
I do this in C#?


No it's not supported (you can apply the OptionalAttribute to a
parameter but can't set the default value, and no parameters are
optional for calling C# code). You typically provide overloads
instead.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 1 '06 #3
Hi,
You can not, you have to use overload methods :

void M( string s , int i){ M( s, i, false);}
void M( string s , int i, bool b)
{ //do stuff
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"deko" <de**@nospam.com> wrote in message
news:ta********************@comcast.com...
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, and
"params" to send in an array, but is it possible to have optional
parameters? How do I do this in C#?

Thanks in advance.

Mar 1 '06 #4
On Wed, 1 Mar 2006 11:33:50 -0800, "deko" <de**@nospam.com> wrote:
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, and "params"
to send in an array, but is it possible to have optional parameters? How do
I do this in C#?

Thanks in advance.


As far as I know, it can't be done like that (but I haven't really
looked for it either --> so don't blame me on that)

What you could do is this:

int function(string this, int that, otherthing bool)
{
// do stuff
}
int function(string this, int that)
{
return function(this, that, false);
}

Two functions with the same name, the other is just called with a
default parameter.

Leon
Mar 1 '06 #5
On Wed, 1 Mar 2006 11:33:50 -0800, "deko" <de**@nospam.com> wrote:
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, and "params"
to send in an array, but is it possible to have optional parameters? How do
I do this in C#?

Thanks in advance.


Probably not as you might normally use the term optional parameters.
Your call must have all the parameters the method calls for, you
cannot leave any out. This said, you can do a pseudo-optional by
allowing either 'null' or "Missing.Value" to be passed and handling it
on a parameter by parameter basis inside the method, i.e.

if (parameterX== null)
parameterX = defaultValue;

This may be more of a headache then it is worth however.

http://support.microsoft.com/default...b;en-us;305814

Ken Wilson
Seeking viable IT employment in Victoria, BC
Mar 1 '06 #6
> Probably not as you might normally use the term optional parameters.
Your call must have all the parameters the method calls for, you
cannot leave any out. This said, you can do a pseudo-optional by
allowing either 'null' or "Missing.Value" to be passed and handling it
on a parameter by parameter basis inside the method, i.e.

if (parameterX== null)
parameterX = defaultValue;

This may be more of a headache then it is worth however.

http://support.microsoft.com/default...b;en-us;305814


That Microsoft article was so poorly written it made me puke. I guess
overloading is the way to go, but then you have repeated code.

What about passing a params array? Could I sometimes pass a 5-element
params array, sometimes a 2-element, and so on, and just handle the
different scenarios with logic in the body of the method?

Mar 1 '06 #7
deko wrote:
I guess overloading is the way to go, but then you have repeated code.
No you wouldn't, the overload would just call the method with all the
params:

void foo(arg1)
{
//Call the version of foo with 2 args, passing null for arg not
specified
foo(arg1, null)
}

void foo(arg1, arg2)
{
//code here
}

What about passing a params array? Could I sometimes pass a 5-element
params array, sometimes a 2-element, and so on, and just handle the
different scenarios with logic in the body of the method?


Yes. The param array does not have to have the same number of
arguments each time.

Mar 1 '06 #8
As someone else already mentioned, you would not have repeated code if you
overload methods. The general rule is that the method with the most
parameters has all the logic, and all other overloaded methods call either
the main method, or another overloaded method. All your logic should be
contained in only one methd. For example (a simple example),

public int InitializeUSBDevice(int venderID, int productID, string command)
{
//Initialize USB device and send initialize command
return successCode
}

public int InitializeUSBDevice(int productID, string command)
{
return this.InitializeUSBDevice(this.defaultVendorID, productID, command);
}

public int InitializeUSBDevice(string command)
{
return this.InitializeUSBDevice(this.defaultProductID, command)
}

public int InitializeUSBDevice()
{
return this.InitializeUSBDevice(this.defaultCommand);
}

In addition, keep in mind that Optional parameters in VB.NET are not CLS
Compliant. If I have a VB.NET DLL with a method:

Public Function Add(ByVal param1 As Integer, ByVal Optional param2 As Integer)
'Do some work and return
Return result
End Function

The optional parameter "param2" would not be optional from a C# win app, or
a managed C++ win app ... or any .NET syntax (I hate using the word
"language"). In another other language other than VB, the second parameter
will always be required.

It's something to think about. Hope this helps.

"deko" wrote:
Probably not as you might normally use the term optional parameters.
Your call must have all the parameters the method calls for, you
cannot leave any out. This said, you can do a pseudo-optional by
allowing either 'null' or "Missing.Value" to be passed and handling it
on a parameter by parameter basis inside the method, i.e.

if (parameterX== null)
parameterX = defaultValue;

This may be more of a headache then it is worth however.

http://support.microsoft.com/default...b;en-us;305814


That Microsoft article was so poorly written it made me puke. I guess
overloading is the way to go, but then you have repeated code.

What about passing a params array? Could I sometimes pass a 5-element
params array, sometimes a 2-element, and so on, and just handle the
different scenarios with logic in the body of the method?

Mar 2 '06 #9
I forgot to answer your second question. It doesn't matter what the size of
the array is when your passing it to a method.

public float GetAverage(int[] integerArray)
{
int sum = 0;

for(int i = 0; i < integerArray.Length; i++)
sum += integerArray[i];

return (float) sum / integerArray.Length;
}

"deko" wrote:
Probably not as you might normally use the term optional parameters.
Your call must have all the parameters the method calls for, you
cannot leave any out. This said, you can do a pseudo-optional by
allowing either 'null' or "Missing.Value" to be passed and handling it
on a parameter by parameter basis inside the method, i.e.

if (parameterX== null)
parameterX = defaultValue;

This may be more of a headache then it is worth however.

http://support.microsoft.com/default...b;en-us;305814


That Microsoft article was so poorly written it made me puke. I guess
overloading is the way to go, but then you have repeated code.

What about passing a params array? Could I sometimes pass a 5-element
params array, sometimes a 2-element, and so on, and just handle the
different scenarios with logic in the body of the method?

Mar 2 '06 #10
> As someone else already mentioned, you would not have repeated code if you
overload methods. The general rule is that the method with the most
parameters has all the logic, and all other overloaded methods call either
the main method, or another overloaded method. All your logic should be
contained in only one methd. For example (a simple example),
So an optional parameter is one that gets it's own stub. I suppose that's
not so bad.
The optional parameter "param2" would not be optional from a C# win app,
or
a managed C++ win app ... or any .NET syntax (I hate using the word
"language"). In another other language other than VB, the second
parameter
will always be required.


good point - it's a bit of a stretch to think of VB and C# as different
languages. More like different .NET syntax.

Mar 2 '06 #11

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

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
2
by: William Ortenberg | last post by:
I have a client who can't get off of Access97. I want to use the InStrRev function, but it's not available in 97. Is the function's code available? Is there any equivalent functions for 97? ...
4
by: Randy | last post by:
Compiling the following code #include <iostream> #include "cenum.h" using namespace std; class Test { CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");
13
by: Jim C | last post by:
Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which reserves the next autoinc value for a table? Yes I know about @@Identity and SCOPE_IDENTITY. I need to get the next autoinc...
6
by: openopt | last post by:
Thank you in advance for your response. Dmitrey
20
by: veebhudhi.chandramouli | last post by:
Instead of using same name i can use different name, is there any specific purpous is there to define the polymorphic overloaded functions in c#
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
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...
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...

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.