optional parameters in a method

G

Guest

hello, friends,

I need to have a few optional parameters in a method of a class and check
them if there are any values passed. I know how to do it in VB.net, but I do
not know how to do it in VC++.net.

Probably it is easy, but could anyone help me with a sample source code or
reference paper for a method that allows optional parameters?

Thanks a lot.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

Optional parameters are not allowed by C#. Rather, you will have to
overload your method. You should probably route the calls to other
overloads so that you don't duplicate business logic.

Hope this helps.
 
G

Guest

Hi Andrew,

This is a C# newgroup. You will get better response by posting @ vc
newsgroups.

ASAIF, unlike VB.NET, there are no optional parameters in VC++. But you
could easiliy accomplish the same by using method overloading.
Eg:
public void MyMethod(int a, int b)
{
}

public void MyMethod(int a)
{
// b was not passed, so call the actual MyMethod with b as 0
MyMethod(a, 0);
}

HTH,
Rakesh Rajan
 

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