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

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 26436
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
If possible, how do I use a function to create a default parameter value? The following gives me an "unexpected '(', expecting ')'" error, which makes sense. function DisplayInfo($ID,...
26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
3
by: Phil IU Guy | last post by:
I am having 2 issues, both acting very randomly, and for the most part I dont get this message on most computers, but I have had a couple computers get either issue 1, or issue 2. Issue #1: I...
2
by: mudman04 | last post by:
Hi, I searched online for some similar issues that I am facing but was not able come up with anything. I am fairly new with Access (2 months experience) and I am trying to remove a message...
0
by: Homer J. Simpson | last post by:
When I try to use the following: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:csToolbar %>" SelectCommand="spGetValuesForDateRange"...
6
by: s0suk3 | last post by:
I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return value of another method of the same class. For example: ...
1
by: s0suk3 | last post by:
I had posted this before but all the spam whipped it out... I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.