473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optional parameter declaration and default values

Hello All,
I am new to C# , how can I delare parameters to a function as optional,
and also how are default values assigned ?

consider the function

public String myFunc(String thisisOptional, String OptWithDefaultV alue)
{

}

Imran.
Nov 17 '05 #1
5 7420
"Imran Aziz" <im***@tb2.ne t> wrote in message
news:uu******** *****@TK2MSFTNG P09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}
Nov 17 '05 #2
I see, it means there is no way to declare optional function parameters in
C#, strange , no doubt I was not able to find it in the documentation :)

Thanks a lot.

Imran.
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
"Imran Aziz" <im***@tb2.ne t> wrote in message
news:uu******** *****@TK2MSFTNG P09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}

Nov 17 '05 #3
Hi,

In this video Anders Hejlsberg, the designer of C#, explains why they
decided to leave optional parameters out of C#:

http://msdn.microsoft.com/msdntv/epi...h/manifest.xml

Regards - Octavio

"Imran Aziz" <im***@tb2.ne t> escribió en el mensaje
news:%2******** *******@TK2MSFT NGP15.phx.gbl.. .
I see, it means there is no way to declare optional function parameters in
C#, strange , no doubt I was not able to find it in the documentation :)

Thanks a lot.

Imran.
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
"Imran Aziz" <im***@tb2.ne t> wrote in message
news:uu******** *****@TK2MSFTNG P09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}


Nov 17 '05 #4
"Imran Aziz" <im***@tb2.ne t> wrote in message
news:%2******** *******@TK2MSFT NGP15.phx.gbl.. .
it means there is no way to declare optional function parameters in C#
Correct.
strange


Nope - entirely by design.
Nov 17 '05 #5
You can't.

Use:
1) Overloads
public String myFunc()
{
myFunc(null, x //default value);
}

public String myFunc(String thisisOptional)
{
myFunc(thisisOp tional, x //default value);
}

public String myFunc(String thisisOptional, String OptWithDefaultV alue)
{
..... code here.
}

2) params object[] parameters like parameter in the method.

public String myFunc(params string[] parameters )
{
String thisisOptional = null;
String OptWithDefaultV alue = defaultValue;

if (params.Lenght >= 2)
{
thisisOptional = parameters[0];
OptWithDefaultV alue = parameters[1];
}
else if (params.Lenght == 1)
thisisOptional = parameters[0];

.... code here
}

Gustavo

"Imran Aziz" <im***@tb2.ne t> wrote in message
news:uu******** *****@TK2MSFTNG P09.phx.gbl...
Hello All,
I am new to C# , how can I delare parameters to a function as optional,
and also how are default values assigned ?

consider the function

public String myFunc(String thisisOptional, String OptWithDefaultV alue)
{

}

Imran.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9506
by: Programmer | last post by:
This may sound easy and it probably is, I just started learning C++. I have a class that writes to a file and I would like to add a second optional parameter that will be used to tell the class if a newline character should be written so the next output will start on a new line. My class looks like this: class Debug { public: Debug();
6
2614
by: Otto Wyss | last post by:
I've the following function declaration: wxTree GetLastChild (const wxTree& item, long& cookie) const; I'd like to make the cookie parameter optional, i.e. "long& cookie = ....", without breaking any software using the old API. The implementation looks like wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item, long& cookie) const {
4
6183
by: Gerry Abbott | last post by:
Hi All, Im trying to use thie combination but have not had success. Below is the function It tried the following myriskLevel(2,2) myrisklevel(0,0,2) and the ismissing(Three) alwasy returns false.?
4
2673
by: MLH | last post by:
The following function declaration confuses me... Public Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean I tried pasting it and its code into an Access 2.0 module. I was unsuccessful. I had to remove the following... "Public" "Optional ByRef" "As Boolean"
21
5772
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code would go something like this: dim qry as dao.querydef set qry = currentdb.querydefs("myquery") qry.parameters("Par1") = "blah"
4
5945
by: Joe HM | last post by:
Hello - I realize that there is no more IsMissing function in VB.NET but how can I have a boolean argument that is optional and in the code I need to determine whether it was passed it or not? I use the Single.NaN to set the default values for some of my optional single arguments so that I can determine whether an argument was passed it ... but that does not work for booleans.
14
3274
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
12
2673
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be avoided. I'd like to hear your various opinions on this matter.
7
7069
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1 As Integer = 0, _ Optional ByVal Arg2 As Integer = 0, _ Optional ByVal Arg3 As Integer = 0) ' Call my SQL proc with the same signature
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
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
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.