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

D

deko

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.
 
M

Mattias Sjögren

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
 
J

Jon Skeet [C# MVP]

deko said:
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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
}
 
L

Leon Friesema

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
 
K

Ken Wilson

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.aspx?scid=kb;en-us;305814

Ken Wilson
Seeking viable IT employment in Victoria, BC
 
D

deko

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.aspx?scid=kb;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?
 
C

Chris Dunaway

deko said:
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.
 
G

Guest

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.
 
G

Guest

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;

return (float) sum / integerArray.Length;
}
 
D

deko

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top