Connecting Tech Pros Worldwide Help | Site Map

what is optional arguments, (definition i want )

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: 3 Weeks Ago
any body?
as soon as possible reply
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#2: 3 Weeks Ago

re: what is optional arguments, (definition i want )


This is really not a question, but what you are referring to is probably overloading.

If it is not not then please post the relevant code for clarification.

PS. Please read this thread for some posting guidlines
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#3: 3 Weeks Ago

re: what is optional arguments, (definition i want )


Optional arguments are arguments which are not required.
Really, this shouldn't have required an explanation
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 253
#4: 3 Weeks Ago

re: what is optional arguments, (definition i want )


Looks like your question has an answer.

:)
:)

Regards
Dheeraj Joshi
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 699
#5: 3 Weeks Ago

re: what is optional arguments, (definition i want )


C# does not support optional parameters. However 4.0 will support optional parameters. For earlier version, you can either pass, "null" or Type.Missing object as parameters
Expand|Select|Wrap|Line Numbers
  1. object o=Type.Missing;
  2. or object o=null;
  3.  
Link
Expand|Select|Wrap|Line Numbers
  1. /For 4.0
  2. public void OptionalParameters(string para, string opPara=" Bytes")
  3. {
  4.  
  5. }
  6. //You can call OptionalParameters either 
  7. OptionalParameters("Hi");
  8. // in this case the opPara=" Bytes" default is assumed
  9.  
  10. OptionaParameters("Hi","Jon");
  11. //here opPara ="Jon", default value is overriden. 
  12.  
Link1
Link2
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: 3 Weeks Ago

re: what is optional arguments, (definition i want )


You can mimic optional parameters (to an extent) with something like
Expand|Select|Wrap|Line Numbers
  1. void myfunction(int required, params object[] OptionalThings)
  2. {
  3. // ....
  4. }
  5.  
  6. //calling both
  7. myfunction(0);
  8. //and
  9. myfunction(1,"fred",23,44,"billy",3.45);
  10. //will work
  11.  
As a side note, to pass null to int/float/etc types, declare your function with int? value, instead of int value
Reply