473,569 Members | 2,652 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(stri ng whatever, string whatever2 = "someval")
Dec 11 '06 #1
4 26450
Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(stri ng whatever)
{
SomeMethod(what ever, "someval");
}

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

E.g.

private void somemethod(stri ng 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("bla h","blah",,, ) or simply SomeMethod("bla h","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*********@di scussions.micro soft.comwrote in message
news:3F******** *************** ***********@mic rosoft.com...
Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(stri ng whatever)
{
SomeMethod(what ever, "someval");
}

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

E.g.

private void somemethod(stri ng 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.microso ft.comwrote in message
news:ec******** ******@TK2MSFTN GP02.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("bla h","blah",,, ) or simply SomeMethod("bla h","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*********@di scussions.micro soft.comwrote in message
news:3F******** *************** ***********@mic rosoft.com...
>Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(stri ng whatever)
{
SomeMethod(what ever, "someval");
}

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

E.g.

private void somemethod(stri ng 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.microso ft.comschreef in bericht
news:ec******** ******@TK2MSFTN GP02.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("bla h","blah",,, ) or simply SomeMethod("bla h","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*********@di scussions.micro soft.comwrote in message
news:3F******** *************** ***********@mic rosoft.com...
>Hi,
you cannot have optional parameters in C#, instead you can use method
overloading i.e.

private void SomeMethod(stri ng whatever)
{
SomeMethod(what ever, "someval");
}

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

E.g.

private void somemethod(stri ng 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
2884
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, $startdate=date("Ydm"), $numdays=7) { ...function here... }
26
15531
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 = myList
12
12685
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
2735
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 default value, which is always_true(). but gcc says: no matching function for call to `x::do_something()' and msvs says: could not deduce...
3
5489
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 have a module that has a function that starts as: Function setFormColors(frm As Form) This function takes a form and looks through its objects and if...
2
5415
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 stating, ENTER PARAMETER VALUE when I am generating a report. I know that the culprit lies in the query stored in the report. I have entered data in the...
0
1724
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" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:Parameter DefaultValue="<%= DateTime.MinValue %>" Name="FIRSTDAY" Type="DateTime" />...
6
1455
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: class A: def __init__(self): self.x = 1 def meth1(self): return self.x
1
1003
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 value of another method of the same class. For example: class A: def __init__(self): self.x = 1
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2114
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 we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.