optional-parameters

avoiding the tedium of optional parameters

血红的双手。 提交于 2019-11-26 09:42:56
问题 If I have a constructor with say 2 required parameters and 4 optional parameters, how can I avoid writing 16 constructors or even the 10 or so constructors I\'d have to write if I used default parameters (which I don\'t like because it\'s poor self-documentation)? Are there any idioms or methods using templates I can use to make it less tedious? (And easier to maintain?) 回答1: You might be interested in the Named Parameter Idiom. To summarize, create a class that holds the values you want to

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

ⅰ亾dé卋堺 提交于 2019-11-26 08:21:25
问题 Both of these generate an error saying they must be a compile-time constant: void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0)) void Foo(TimeSpan span = new TimeSpan(2000)) First of all, can someone explain why these values can\'t be determined at compile time? And is there a way to specify a default value for an optional TimeSpan object? 回答1: You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2);

Any way to specify optional parameter values in PHP?

拈花ヽ惹草 提交于 2019-11-26 08:09:40
问题 Let\'s say I\'ve got a PHP function foo: function foo($firstName = \'john\', $lastName = \'doe\') { echo $firstName . \" \" . $lastName; } // foo(); --> john doe Is there any way to specify only the second optional parameter? Example: foo($lastName=\'smith\'); // output: john smith 回答1: PHP does not support named parameters for functions per se. However, there are some ways to get around this: Use an array as the only argument for the function. Then you can pull values from the array. This

Can I have an optional parameter for an ASP.NET SOAP web service

天涯浪子 提交于 2019-11-26 07:48:01
问题 I want to build a webservice with this signature, which does not throw an exception if param2 is left empty. Is this possible? [WebMethod] public string HelloWorld(string param1, bool param2) { } The exception is a System.ArgumentException that is thrown when trying to convert the empty string to boolean. Ideas that have not worked so far: method overloading is not allowed for webservices, like public string HelloWorld(string param1) { return HelloWorld(param1, false); } as suggested here:

Default value of function parameter

故事扮演 提交于 2019-11-26 05:55:57
问题 1. int Add (int a, int b = 3); int Add (int a, int b) { } 2. int Add (int a, int b); int Add (int a, int b = 3) { } Both work; which is the standard way and why ? 回答1: If you put the declaration in a header file, and the definition in a separate .cpp file, and #include the header from a different .cpp file, you will be able to see the difference. Specifically, suppose: lib.h int Add(int a, int b); lib.cpp int Add(int a, int b = 3) { ... } test.cpp #include "lib.h" int main() { Add(4); } The

Should you declare methods using overloads or optional parameters in C# 4.0?

不打扰是莪最后的温柔 提交于 2019-11-26 05:26:37
问题 I was watching Anders\' talk about C# 4.0 and sneak preview of C# 5.0, and it got me thinking about when optional parameters are available in C# what is going to be the recommended way to declare methods that do not need all parameters specified? For example something like the FileStream class has about fifteen different constructors which can be divided into logical \'families\' e.g. the ones below from a string, the ones from an IntPtr and the ones from a SafeFileHandle . FileStream(string

How would I skip optional arguments in a function call?

喜欢而已 提交于 2019-11-26 05:17:27
OK I totally forgot how to skip arguments in PHP. Lets say I have: function getData($name, $limit = '50', $page = '1') { ... } How would I call this function so that the middle parameter takes the default value (ie. '50')? getData('some name', '', '23'); Would the above be correct? I can't seem to get this to work. Your post is correct. Unfortunately, if you need to use an optional parameter at the very end of the parameter list, you have to specify everything up until that last parameter. Generally if you want to mix-and-match, you give them default values of '' or null , and don't use them

Conflicting overloaded methods with optional parameters

我怕爱的太早我们不能终老 提交于 2019-11-26 05:16:14
I have two overloaded methods, one with an optional parameter. void foo(string a) { } void foo(string a, int b = 0) { } now I call: foo("abc"); interestingly the first overload is called. why not the second overload with optional value set to zero? To be honest, I would have expect the compiler to bring an error, at least a warning to avoid unintentional execution of the wrong method. What's the reason for this behaviour? Why did the C# team define it that way? From MSDN : If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for

C# 4.0, optional parameters and params do not work together

感情迁移 提交于 2019-11-26 04:52:23
问题 How can i create a method that has optional parameters and params together? static void Main(string[] args) { TestOptional(\"A\",C: \"D\", \"E\");//this will not build TestOptional(\"A\",C: \"D\"); //this does work , but i can only set 1 param Console.ReadLine(); } public static void TestOptional(string A, int B = 0, params string[] C) { Console.WriteLine(A); Console.WriteLine(B); Console.WriteLine(C.Count()); } 回答1: Your only option right now is to overload the TestOptional (as you had to do

Is there a way to provide named parameters in a function call in JavaScript?

孤街醉人 提交于 2019-11-26 03:16:09
问题 I find the named parameters feature in C# quite useful in some cases. calculateBMI(70, height: 175); What can I use if I want this in JavaScript? What I don’t want is this: myFunction({ param1: 70, param2: 175 }); function myFunction(params){ // Check if params is an object // Check if the parameters I need are non-null // Blah blah } That approach I’ve already used. Is there another way? I’m okay using any library to do this. 回答1: ES2015 and later In ES2015, parameter destructuring can be