469,306 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Default Parameter Value ?

I there a way to provide a default value for a parameter in a method?

E.g.

private void somemethod(string whatever, string whatever2 = "someval")
Dec 11 '06 #1
4 26304
Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(string whatever)
{
SomeMethod(whatever, "someval");
}

private void SomeMethod(string whatever, string whatever2)
{
//implement
}
Mark.
--
http://www.markdawson.org
"msnews.microsoft.com" wrote:
I there a way to provide a default value for a parameter in a method?

E.g.

private void somemethod(string whatever, string whatever2 = "someval")
Dec 11 '06 #2
Thanks Mark.

Another quick question. I am calling methods from a COM object. These
methods contain optional parameters... in VB.NET I do not have to supply
values at all, just have to leave them blank. E.g.
SomeMethod("blah","blah",,,) or simply SomeMethod("blah","blah") . however,
in C# I receive a "no overload for method" error during compile. Why is it
in VB.NET I do not have to supply values but I must in C#? Is there a work
around?

Thanks a lot.

"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(string whatever)
{
SomeMethod(whatever, "someval");
}

private void SomeMethod(string whatever, string whatever2)
{
//implement
}
Mark.
--
http://www.markdawson.org
"msnews.microsoft.com" wrote:
>I there a way to provide a default value for a parameter in a method?

E.g.

private void somemethod(string whatever, string whatever2 = "someval")
Dec 11 '06 #3
Hi,

In C# you must supply all parameters, including the COM-optional ones, so
Type.Missing is commonly used in cases like yours.

You may want to create a class that encapsulates the required functionality
of the COM object to provide a more .NET-centric wrapper for callers. You
can use Mark's method overloading suggestion to provide optional arguments
in C#. Although, many legacy COM classes provide an extraordinary number of
optional parameters (as in Office automation, for example) - too many to
create overloads for each possibility. In those cases it's important to
only wrap the functionality that you'll need.

--
Dave Sexton

<msnews.microsoft.comwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
Thanks Mark.

Another quick question. I am calling methods from a COM object. These
methods contain optional parameters... in VB.NET I do not have to supply
values at all, just have to leave them blank. E.g.
SomeMethod("blah","blah",,,) or simply SomeMethod("blah","blah") .
however, in C# I receive a "no overload for method" error during compile.
Why is it in VB.NET I do not have to supply values but I must in C#? Is
there a work around?

Thanks a lot.

"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
>Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(string whatever)
{
SomeMethod(whatever, "someval");
}

private void SomeMethod(string whatever, string whatever2)
{
//implement
}
Mark.
--
http://www.markdawson.org
"msnews.microsoft.com" wrote:
>>I there a way to provide a default value for a parameter in a method?

E.g.

private void somemethod(string whatever, string whatever2 = "someval")

Dec 11 '06 #4
If it goes in VB.Net with nothing (not the keyword) than it goes most
probably in C# with null, null, null, etc.

Cor

<msnews.microsoft.comschreef in bericht
news:ec**************@TK2MSFTNGP02.phx.gbl...
Thanks Mark.

Another quick question. I am calling methods from a COM object. These
methods contain optional parameters... in VB.NET I do not have to supply
values at all, just have to leave them blank. E.g.
SomeMethod("blah","blah",,,) or simply SomeMethod("blah","blah") .
however, in C# I receive a "no overload for method" error during compile.
Why is it in VB.NET I do not have to supply values but I must in C#? Is
there a work around?

Thanks a lot.

"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
>Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(string whatever)
{
SomeMethod(whatever, "someval");
}

private void SomeMethod(string whatever, string whatever2)
{
//implement
}
Mark.
--
http://www.markdawson.org
"msnews.microsoft.com" wrote:
>>I there a way to provide a default value for a parameter in a method?

E.g.

private void somemethod(string whatever, string whatever2 = "someval")

Dec 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike | last post: by
26 posts views Thread by Alex Panayotopoulos | last post: by
12 posts views Thread by earl | last post: by
3 posts views Thread by Capstar | last post: by
6 posts views Thread by s0suk3 | last post: by
1 post views Thread by s0suk3 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.