optional-parameters

Optional query parameters for Android Room

谁说胖子不能爱 提交于 2019-12-06 07:04:54
I have the following DAO with a query: @Dao public interface BaseballCardDao { @Query( "SELECT * FROM baseball_cards " + "WHERE brand LIKE :brand " + " AND year = :year " + " AND number LIKE :number " + " AND player_name LIKE :playerName " + " AND team LIKE :team" ) LiveData<List<BaseballCard>> getBaseballCards( String brand, int year, String number, String playerName, String team ); } The String parameters are "optional" in the sense that I can pass "%%" to match all rows due to the LIKE operator. But I cannot do this with year since it is an int . One solution is to add two different @Query

C# Selecting a different method to the one expected with optional parameters and multiple levels of inheritance

一笑奈何 提交于 2019-12-06 04:18:53
In the given example (sorry for code style, it was in the interest of a compact self-contained repro), the method called is always the virtual method in B that has the additional integer parameter, while I would expect that the method implemented in C would be the one called. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodChoosing { class Program { static void Main(string[] args) { C c = new C(); c.M(); Console.ReadKey(); } } public class A { public virtual void M(bool? a = null) { Console.WriteLine("base base"); } } public class B : A {

java optional parameters [duplicate]

假装没事ソ 提交于 2019-12-05 18:01:21
This question already has answers here : Java optional parameters (16 answers) Closed 5 months ago . I want to write an average method in java such that it can consume N amount of items, returning the average of them: My idea was: public static int average(int[] args){ int total = 0; for(int i=0;i<args.length;i++){ total = total + args[i]; } return Math.round (total/args.length); } //test it average(1,2,3) // s**hould return 2. how can I change my method to consume any amount of parameters instead of int[] args so can work the way I want ? Cheers Java 5 supports varargs , which is what you

C# Optional Array Parameter for Class

拜拜、爱过 提交于 2019-12-05 12:21:27
问题 I know this can be done using null so I have a workaround for that, but I was wondering if there was a better way that I can have an optional int[] parameter for a class? class PriceLevels { public int[] priceLevels { get; } private readonly int[] defaultPriceLevels = { 2, 3, 3, 4, 5, 6 }; public PriceLevels(int[] newPriceLevels = defaultPriceLevels) { priceLevels = newPriceLevels; } } This gives me an error saying it is an invalid expression defaultPriceLevels must be constant. How can I fix

Use “Optional, DefaultParameterValue” attribute, or not?

荒凉一梦 提交于 2019-12-05 12:05:51
问题 Is there any difference between using Optional and DefaultParameterValue attributes and not using them? public void Test1([Optional, DefaultParameterValue("param1")] string p1, [Optional, DefaultParameterValue("param2")] string p2) { } public void Test2(string p1= "param1", string p2= "param2") { } both work: Test1(p2: "aaa"); Test2(p2: "aaa"); 回答1: The difference is that by using the attributes explicitly, the compiler doesn't enforce the same strictness on type requirements. public class C

OptionalAttribute parameter's default value?

﹥>﹥吖頭↗ 提交于 2019-12-05 11:14:48
问题 MSDN's VS2010 Named and Optional Arguments (C# Programming Guide) tells us about optional parameters in C#, showing code like I'd expect: public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) Ok, but it also says: You can also declare optional parameters by using the .NET OptionalAttribute class. OptionalAttribute parameters do not require a default value. I read MSDN's OptionalAttribute page, and done searches online (which shows lots of people

How do i set an optional parameter in PHP after the first optional parameter

╄→гoц情女王★ 提交于 2019-12-05 09:15:14
I'm fairly new to php and I am trying to figure how do I set an optional parameter after the first optional parameter? For example I have the following code: function testParam($fruit, $veg='pota',$test='default test'){ echo '<br>$fruit = '.$fruit; echo '<br>$veg = '.$veg; echo '<br>Test = '.$test; } If i make the following calls: echo 'with all parama'; testParam('apple','carrot','some string'); //we get: //with all parama //$fruit = apple //$veg = carrot //Test = some string echo '<hr> missing veg'; testParam('apple','','something'); //we get: //missing veg //$fruit = apple //$veg = //Test =

Optional Argument code compiles in .NET 3.5. Why?

安稳与你 提交于 2019-12-05 08:46:15
This piece of code compiles OK in VS 2010 in a framework 3.5 project (I triple checked that) public LoggingClient(string uri = "net.msmq://localhost/logging"){...} Why? I see nothing in the C# 4 spec ( doc version ), section 21.1, that says this should be backwardly compatible. How is it that I get no compilation error? Will this fail silently in some circumstances? Project + Properties, Build tab, scroll down, Advanced. You can change the Language Version to "C# 3.0" if you prefer to maintain source code compatibility. But yes, you are using the C# 4.0 compiler in VS2010, regardless of the

Doxygen - declare parameter as optional

三世轮回 提交于 2019-12-05 05:23:00
I am documenting a codebase using Doxygen and was wondering if there is a keyword for declaring an argument to a function to be optional. Something like: /*! \fn int add(int a, int b=0) \brief adds two values \param a the first operand \param \optional b the second operand. Default is 0 \return the result */ It seems like this is something that should exist, but I haven't been able to find it anywhere. Is there an actual option, or do I just need to make note in the description? Arne Mertz To answer your question: No, there is no such thing. But I don't share your opinion that there should be

How can I create an optional DateTime parameter?

北城余情 提交于 2019-12-05 01:02:57
I have this function that returns a reference type. Now, this function has two optional parameters both of which are instances of the DateTime class. The function is something like this: public DateTime GetDate(DateTime start = DateTime.MinValue, DateTime end = DateTime.MinValue) { // Method body... } The error from VS is: Default parameter value for 'start' must be a compile-time constant Of course, the error applies to the second parameter and I perfectly understand what is happening. What I really want is to know if there is a way to go about this, that is, having optional parameters in the