473,505 Members | 13,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26443
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
2878
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
15515
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
12680
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
2724
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
5485
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
5412
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
1723
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
1449
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
1001
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...
0
7098
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...
1
7017
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
7471
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
5610
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,...
1
5026
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
4698
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.