optional-parameters

avoiding the tedium of optional parameters

独自空忆成欢 提交于 2019-11-27 01:36:32
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?) You might be interested in the Named Parameter Idiom . To summarize, create a class that holds the values you want to pass to your constructor(s). Add a method to set each of those values, and have each method do a return *this;

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

纵然是瞬间 提交于 2019-11-26 22:31:12
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? You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } ... } I should elaborate - the reason those expressions in your example are not compile-time constants is

optional parameters in SQL Server stored proc?

笑着哭i 提交于 2019-11-26 22:14:42
问题 I'm writing some stored procs in SQL Server 2008, and wondered if the concept of optional input parameters is possible here? I suppose I could always pass in NULL for parameters I don't want to use, check the value in the stored proc, then take things from there, but I was interested if the concept is available here. Thanks! 回答1: You can declare like this CREATE PROCEDURE MyProcName @Parameter1 INT = 1, @Parameter2 VARCHAR (100) = 'StringValue', @Parameter3 VARCHAR (100) = NULL AS /* check

Any way to specify optional parameter values in PHP?

一世执手 提交于 2019-11-26 22:02:47
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 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 allows for using named arguments in the array. If you want to allow optional number of arguments depending on context,

Can a Delegate have an optional parameter?

江枫思渺然 提交于 2019-11-26 21:34:17
问题 I have the below code that was working fine until I tried adding the bool NetworkAvailable = true portion. Now I get a Method name expected compile time exception at Line 4 below. void NetworkStatus_AvailabilityChanged(object sender, NetworkStatusChangedArgs e) { var networkAvailable = e.IsAvailable; SetUpdateHUDConnectedMode d = new SetUpdateHUDConnectedMode(UpdateHUDConnectedMode(networkAvailable)); this.Invoke(d); } delegate void SetUpdateHUDConnectedMode(bool NetworkAvailable = true);

method overloading vs optional parameter in C# 4.0 [duplicate]

我与影子孤独终老i 提交于 2019-11-26 19:39:31
This question already has an answer here: Should you declare methods using overloads or optional parameters in C# 4.0? 13 answers which one is better? at a glance optional parameter seems better (less code, less XML documentation, etc), but why do most MSDN library classes use overloading instead of optional parameters? Is there any special thing you have to take note when you choose to use optional parameter (or overloading)? One good use case for 'Optional parameters' in conjunction with 'Named Parameters' in C# 4.0 is that it presents us with an elegant alternative to method overloading

C# 4.0 optional out/ref arguments

ε祈祈猫儿з 提交于 2019-11-26 19:39:16
Does C# 4.0 allow optional out or ref arguments? As already mentioned, this is simply not allowed and I think it makes a very good sense. However, to add some more details, here is a quote from the C# 4.0 Specification , section 21.1: Formal parameters of constructors, methods, indexers and delegate types can be declared optional: fixed-parameter: attributes opt parameter-modifier opt type identifier default-argument opt default-argument: = expression A fixed-parameter with a default-argument is an optional parameter , whereas a fixed-parameter without a default-argument is a required

Should these arguments be added or removed?

怎甘沉沦 提交于 2019-11-26 18:34:18
问题 When Resharper argues with itself, how does one know which persona to give more credence? I think I have found some code that does confuse Resharper (this is apparently a very unusual case - after using it for a day, I think Resharper is the bee's knees/the greatest thing since liquified bread, etc.). With this line of code: ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); Resharper tells me to "add argument name 'rgbkey'" and then

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

六月ゝ 毕业季﹏ 提交于 2019-11-26 18:05:00
I noticed that with the optional parameters in C# 4 if you specify a parameter as optional on an interface you DON'T have to make that parameter optional on any implementing class: public interface MyInterface { void TestMethod(bool flag = false); } public class MyClass : MyInterface { public void TestMethod(bool flag) { Console.WriteLine(flag); } } and therefore: var obj = new MyClass(); obj.TestMethod(); // compiler error var obj2 = new MyClass() as MyInterface; obj2.TestMethod(); // prints false Does anyone know why optional parameters are designed to work this way? On one hand I suppose

PHP Optional Parameters - specify parameter value by name?

元气小坏坏 提交于 2019-11-26 17:50:22
问题 I know it is possible to use optional arguments as follows: function doSomething($do, $something = "something") { } doSomething("do"); doSomething("do", "nothing"); But suppose you have the following situation: function doSomething($do, $something = "something", $or = "or", $nothing = "nothing") { } doSomething("do", $or=>"and", $nothing=>"something"); So in the above line it would default $something to "something", even though I am setting values for everything else. I know this is possible