optional-parameters

How would I skip optional arguments in a function call?

孤者浪人 提交于 2019-11-26 02:13:56
问题 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. 回答1: 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

Conflicting overloaded methods with optional parameters

左心房为你撑大大i 提交于 2019-11-26 01:54:17
问题 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? 回答1: From MSDN: If two

How can you use optional parameters in C#?

三世轮回 提交于 2019-11-26 01:37:13
问题 Note: This question was asked at a time when C# did not yet support optional parameters (i.e. before C# 4). We\'re building a web API that\'s programmatically generated from a C# class. The class has method GetFooBar(int a, int b) and the API has a method GetFooBar taking query params like &a=foo &b=bar . The classes needs to support optional parameters, which isn\'t supported in C# the language. What\'s the best approach? 回答1: Surprised no one mentioned C# 4.0 optional parameters that work

Java optional parameters

狂风中的少年 提交于 2019-11-26 01:30:35
问题 How do I use optional parameters in Java? What specification supports optional parameters? 回答1: varargs could do that (in a way). Other than that, all variables in the declaration of the method must be supplied. If you want a variable to be optional, you can overload the method using a signature which doesn't require the parameter. private boolean defaultOptionalFlagValue = true; public void doSomething(boolean optionalFlag) { ... } public void doSomething() { doSomething

How can I use optional parameters in a T-SQL stored procedure?

你说的曾经没有我的故事 提交于 2019-11-25 23:20:43
问题 I am creating a stored procedure to do a search through a table. I have many different search fields, all of which are optional. Is there a way to create a stored procedure that will handle this? Let\'s say I have a table with four fields: ID, FirstName, LastName and Title. I could do something like this: CREATE PROCEDURE spDoSearch @FirstName varchar(25) = null, @LastName varchar(25) = null, @Title varchar(25) = null AS BEGIN SELECT ID, FirstName, LastName, Title FROM tblUsers WHERE

What does the construct x = x || y mean?

依然范特西╮ 提交于 2019-11-25 22:18:33
问题 I am debugging some JavaScript, and can\'t explain what this || does? function (title, msg) { var title = title || \'Error\'; var msg = msg || \'Error on Request\'; } Can someone give me an hint, why this guy is using var title = title || \'ERROR\' ? I sometimes see it without a var declaration as well. 回答1: It means the title argument is optional. So if you call the method with no arguments it will use a default value of "Error" . It's shorthand for writing: if (!title) { title = "Error"; }

Normal arguments vs. keyword arguments

心已入冬 提交于 2019-11-25 22:07:54
问题 How are \"keyword arguments\" different from regular arguments? Can\'t all arguments be passed as name=value instead of using positional syntax? 回答1: there are two related concepts, both called "keyword arguments". On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which