optional-parameters

How To pass Optional Arguments

一个人想着一个人 提交于 2019-12-10 13:17:31
问题 I have one function having two fixed arguments. But next arguments are not fixed, there can be two or three or four of them. It's a runtime arguments so how can I define that function? My code looks like: public ObservableCollection<ERCErrors> ErrorCollectionWithValue (string ErrorDode, int MulCopyNo, dynamic arguments comming it should be 2 or 3) { return null; } 回答1: 1) params (C# Reference) public ObservableCollection<ERCErrors>ErrorCollectionWithValue (string ErrorDode, int MulCopyNo,

Unable to infer generic type with optional parameters

偶尔善良 提交于 2019-12-10 13:06:41
问题 Given the following method signature, why is it when a parameter is explicitly named the compiler is unable to automatically infer the type? Visual Studio 2010 SP1 is able to infer the type and shows no warnings or errors. IEnumerable<T> ExecuteCommand<T>( string commandText, string connectionName = null, Func<IDataRecord, T> converter = null) { ... } static SomeClass Create(IDataRecord record) { return new SomeClass(); } void CannotInferType() { var a = ExecuteCommand( "SELECT blah",

How do I get default values of optional parameters?

[亡魂溺海] 提交于 2019-12-10 11:28:59
问题 I have a constructor with optional parameters. I would like to have an expression to invoke that constructor without providing the optional arguments (I mean let the object be constructed with default values of the parameters). I read here An expression tree may not contain a call or invocation that uses optional arguments that this is not possible. I mean var ctorInfo = getIt; var f = Expression.Lambda<Func<T>>(Expression.New(ctorInfo)).Compile(); fails with System

Optional query parameters for Android Room

落花浮王杯 提交于 2019-12-10 10:56:24
问题 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

Optional parameters in AppleScript handlers

我的梦境 提交于 2019-12-10 09:26:10
问题 The Applescript documentation says that as of Yosemite, parameters for handlers can be made optional. From the section 'Parameter Specifications': Labeled parameters may be declared with a default value by following the formal parameter name with :literal. Doing so makes the parameter optional when called. For example, this declares a make handler with a default value for the with data parameter: on make new theClass with data theData : missing value This handler can now be called without

Doxygen - declare parameter as optional

风格不统一 提交于 2019-12-10 03:36:49
问题 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? 回答1: To

Variable number of method parameters in Objective C - Need an example

家住魔仙堡 提交于 2019-12-09 01:30:21
问题 From Objective C Programming Guide (Under the "Object Messaging" section), Methods that take a variable number of parameters are also possible, though they’re somewhat rare. Extra parameters are separated by commas after the end of the method name. (Unlike colons, the commas are not considered part of the name.) In the following example, the imaginary makeGroup: method is passed one required parameter (group) and three parameters that are optional: [receiver makeGroup:group, memberOne,

Optional parameter in method not compile-time constant error with Rectangle

我的梦境 提交于 2019-12-08 20:14:25
问题 I have a method where I'd like to use a Rectangle optional parameter with default value of (1,1,1,1). void Method(int i, int j = 1, Rectangle rect = new Rectangle(1,1,1,1)) {} //error How do I resolve this? (I'm using XNA, so it's a Microsoft.Xna.Framework.Rectangle .) 回答1: You don't. optional parameters must be compile time constants, and new Rectangle(1,1,1,1) isn't a compile time constant. You could have two method overloads, one that doesn't have a rectangle: void Method(int i, int j = 1)

Does C# 4.0 and a combination of optional parameters and overloads give you a warning about ambiguity?

做~自己de王妃 提交于 2019-12-08 19:38:07
问题 I've started reading Jon Skeet's early access version of his book, which contains sections on C# 4.0, and one thing struck me. Unfortunately I don't have Visual Studio 2010 available so I thought I'd just ask here instead and see if anyone knew the answer. If I have the following code, a mixture of existing code, and new code: public void SomeMethod(Int32 x, Int32 y) { ... } public void SomeMethod(Int32 x, Int32 y, Int32 z = 0) { ... } Will the compiler complain either at the definition site

How to add an optional parameters/default value parameters in VB function?

拥有回忆 提交于 2019-12-08 14:26:10
问题 How can I create a method that has optional parameters in it in Visual Basic? 回答1: Use the Optional keyword and supply a default value. Optional parameters must be the last parameters defined, to avoid creating ambiguous functions. Sub MyMethod(ByVal Param1 As String, Optional ByVal FlagArgument As Boolean = True) If FlagArgument Then 'Do something special Console.WriteLine(Param1) End If End Sub Call it like this: MyMethod("test1") Or like this: MyMethod("test2", False) 回答2: Have in mind