optional-parameters

call C# method from powershell without supplying optional arguments

旧巷老猫 提交于 2019-11-28 01:16:20
问题 I have a c# method I am loading from a dll with optional string arguments that default to null . For example public void foo(string path, string new_name = null, bool save_now = true) { if(name == null) new_name = Path.GetFileNameWithoutExtension(path); ... if(save_now) Save(); } I want to call this from within a powershell script and not supply a value for new_name but one for save_now . As per this seemingly very similar question I have tried $default = [type]::Missing $obj.foo($path,

Can a Delegate have an optional parameter?

↘锁芯ラ 提交于 2019-11-27 23:42:28
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); private void UpdateHUDConnectedMode(bool NetworkAvailable = true) { ... } I am, admittedly, new to

Why optional parameters must appear at the end of the declaration

好久不见. 提交于 2019-11-27 23:29:38
问题 In all programming languages supporting optional parameters that I have seen there is a imitation that the optional parameters must appear at the end of the declaration. No required parameters may be included after an optional item. What is the reason for that ? I guess it can be compiler/interpreter requirement. 回答1: Well, if they were at the front, how would you detect when they've stopped being supplied? The only way would be if the variable type was different after the optional parameters

How do optional Parameters in Let/Get Properties work?

心已入冬 提交于 2019-11-27 20:50:02
问题 I am using vba with Excel 2007, and am writing code for a class module. 1) Is the following code even possible?... Essentially I have two enums, call them eDATASET and eDATATSUBSET . A particular value from eDATASET should trigger an assignment from the optionally passed parameter in a Let property. Something like this: Public Property Let foo(Optional ByVal lngSubSet as eDATASUBSET, _ ByVal lngSuperSet as eDATASET) Select Case lngSuperSet Case eDATASET.abc, eDATASET.def mlngBar = lngSuperSet

Passing optional parameter by reference in c++

时光怂恿深爱的人放手 提交于 2019-11-27 19:20:20
I'm having a problem with optional function parameter in C++ What I'm trying to do is to write function with optional parameter which is passed by reference, so that I can use it in two ways (1) and (2), but on (2) I don't really care what is the value of mFoobar . I've tried such a code: void foo(double &bar, double &foobar = NULL) { bar = 100; foobar = 150; } int main() { double mBar(0),mFoobar(0); foo(mBar,mFoobar); // (1) cout << mBar << mFoobar; mBar = 0; mFoobar = 0; foo(mBar); // (2) cout << mBar << mFoobar; return 0; } but it crashes at void foo(double &bar, double &foobar = NULL) with

Cannot use String.Empty as a default value for an optional parameter

廉价感情. 提交于 2019-11-27 18:04:00
I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate Initialization Logic , he shows the following example of using the new optional parameters feature in a constructor: public MyClass(int initialCount = 0, string name = "") Notice that he used "" instead of string.Empty . He comments: You'll note [in an example above] that the second constructor specified "" for the default value on the name parameter, rather than the more customary string.Empty . That's because string.Empty is not a compile-time constant. It is a static property defined in the string class. Because it is

Should these arguments be added or removed?

半腔热情 提交于 2019-11-27 16:08:55
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 "add argument name 'rgbIV'" So that the line is then: ICryptoTransform Encryptor = RijndaelCipher

Why can't I give a default value as optional parameter except null?

£可爱£侵袭症+ 提交于 2019-11-27 15:45:09
问题 I want to have a optional parameter and set it to default value that I determine, when I do this: private void Process(Foo f = new Foo()) { } I'm getting the following error ( Foo is a class): 'f' is type of Foo, A default parameter of a reference type other than string can only be initialized with null. If I change Foo to struct then it works but with only default parameterless constructor. I read the documentation and it's clearly states that I cannot do this but it doesn't mention why? ,

Check inside method whether some optional argument was passed

我只是一个虾纸丫 提交于 2019-11-27 15:33:17
How do I check if an optional argument was passed to a method? public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint was passed) return; } Another approach is to use Nullable<T>.HasValue ( MSDN definitions , MSDN examples ): int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } Well, arguments are always passed. Default parameter values just ensure that the user doesn't have to explicitly specify them

Guava Optional as method argument for optional parameters

旧街凉风 提交于 2019-11-27 15:23:24
问题 Recently I had a discussion with my teammate on use of Guava Optional for optional parameters in a method. Let's say method is List<Book> getBooks(String catalogId, Optional<String> categoryId) { Validate.notNull(catalogId); Validate.notNull(categoryId); // Point of conflict. Is this required? which accepts a catalogId and an optional categoryId which returns books listed in that catalog and if category is also passed then returns books only in that category. Point of conflict was, Validating