473,326 Members | 2,438 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,326 software developers and data experts.

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 OptWithDefaultValue)
{

}

Imran.
Nov 17 '05 #1
5 7396
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.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****************@TK2MSFTNGP12.phx.gbl...
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.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.net> escribió en el mensaje
news:%2***************@TK2MSFTNGP15.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****************@TK2MSFTNGP12.phx.gbl...
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.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.net> wrote in message
news:%2***************@TK2MSFTNGP15.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(thisisOptional, x //default value);
}

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

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

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

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

.... code here
}

Gustavo

"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.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 OptWithDefaultValue)
{

}

Imran.

Nov 17 '05 #6

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

Similar topics

2
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...
6
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...
4
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...
4
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...
21
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...
4
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? ...
14
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...
12
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...
7
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.