Subscribe to Subscribers

Online Members

Kapil
More...

Forums » .NET » .NET »

Create a method with optional parameters


Posted Date: 01 Feb 2012      Posted By:: Aman Gandhi     Member Level: Gold    Member Rank: 402     Points: 1   Responses: 7



how can i create a method with optional parameters.

i want to create a method where we can left blank an argument.



Aman Gandhi
comman sense is not so comman........




Responses

#654880    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 2

You must create 2 overloaded Method so that when the there is no parameters you use the Method for example


public string add()
{
return "hello";
}

public string add(string a)
{
return a;
}

public string add(string a, string b)
{
return a+b;
}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM


 
#654881    Author: Paritosh Mohapatra      Member Level: Gold      Member Rank: 6     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

You can use Method Overloading concept to create a method with optional parameter

The signature of a method is determined by 3 factors:

a) Number of arguments received by the function.
b) Data types of the parameters/arguments.
c) Position/order of the arguments.


Example of Method Overloading
-----------------------------



protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<br />" + AddNumbers().ToString());
Response.Write("<br />" + AddNumbers(1, 2).ToString());
Response.Write("<br />" + AddNumbers(1, 2, 3).ToString());
}

public int AddNumbers()
{
return 0;
}

public int AddNumbers(int FirstNumber, int SecondNumber)
{
return FirstNumber + SecondNumber;
}

public int AddNumbers(int FirstNumber, int SecondNumber, int ThirdNumber)
{
return FirstNumber + SecondNumber + ThirdNumber;
}



Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM


 
#654883    Author: Aman Gandhi      Member Level: Gold      Member Rank: 402     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 1

is there is any another method to create a optional parameter without using the overloading concept.

Aman Gandhi
comman sense is not so comman........





 
#654884    Author: Nandhini      Member Level: Gold      Member Rank: 343     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 2

No optional parameters in c#.
We can achieve this by setting the default value:


Private void Testing(int i=0,string name)
{
//Implement the code
}


Call this method as below


Testing("Name1");
Testing(1,"Name2");



 
#654886    Author: Ramesh N D      Member Level: Gold      Member Rank: 432     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi Cloud

The above one can be achived in Framework 4.0, this is for your information.


Regards
Ramesh.N.D


 
#654900    Author: Paritosh Mohapatra      Member Level: Gold      Member Rank: 6     Date: 01/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

You can use params keyword.

Please check the following code:


protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br />" + AddNumbers(1, 2, 3));
Response.Write("<br />" + AddNumbers(5, 6));
Response.Write("<br />" + AddNumbers());
}

public int AddNumbers(params int[] numbers)
{
int total = 0;
foreach (int num in numbers)
{
total += num;
}
return total;
}



Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM


 
#655484    Author: Naveen Reddy      Member Level: Gold      Member Rank: 15     Date: 04/Feb/2012   Rating: 2 out of 52 out of 5     Points: 2

Hello,

We have feature in .NET4.0 optional parameter.Please look the below code.


//Optional parameter using C# 4.0
public int Add(int Number1, int Number2, int Number3 = 0)
{
return Number1 + Number2 + Number3;
}


Regards,
Naveen


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Set Environment variable for .net and how to use iis
Previous : How to make update and delete
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.