optional-parameters

What is the difference between OptionalAttribute and optional parameters in C# 4.0

孤人 提交于 2021-02-07 13:30:36
问题 I'm researching someone else's code and there is a method like this: public SomeClass DoSomething(string param1, [Optional, DefaultParameterValue("")] string optional) Why would someone use these attributes instead of public SomeClass DoSomething(string param1, string optional = "") Is there any difference in the behavior, etc.? 回答1: If they weren't using C# 4, for example? I believe the second version will compile into exactly the first version... (I've compiled them both and run them

Initializer for conditional binding must have Optional type, not '[String : Any]'

社会主义新天地 提交于 2021-02-05 10:53:45
问题 Can anyone tell me how to fix this? Im just trying to receive signals from thing speak. `self.title = "Home" print("Requesting data...") Alamofire.request( "https://api.thingspeak.com/channels/290427/feeds.json", parameters: ["results": "1", "location": "false"]) // Gets the latest info from ThingSpeak .responseJSON { response in print("Data downloaded: \(response.result)") if let json = response.result.value as! [String:Any] { print(json) //see full data if let feeds = json["feeds"] as?

Why is String.Empty an invalid default parameter?

╄→гoц情女王★ 提交于 2021-02-05 06:52:10
问题 If I type the following: public Response GetArticles(string Filter = String.Empty) { //Body } Visual Studio gives me this error: Default parameter value for 'Filter' must be a compile-time constant If I change the String.Empty to the classic "" it is fixed. But I'm still curious about what is wrong with the String.Empty and its behavior. 回答1: Why is String.Empty an invalid default parameter? Because "Default parameter value for 'Filter' must be a compile-time constant". And String.Empty is

How can you use optional parameters in C#?

三世轮回 提交于 2021-01-29 18:01:06
问题 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 like

Python argparse requiring option, depending on the defined flags

…衆ロ難τιáo~ 提交于 2021-01-28 04:21:19
问题 I have a small python script, which uses argparse to let the user define options. It uses two flags for different modes and an argument to let the user define a file. See the simplified example below: #!/usr/bin/python3 import argparse from shutil import copyfile def check_file(f): # Mock function: checks if file exists, else "argparse.ArgumentTypeError("file not found")" return f def main(): aFile = "/tmp/afile.txt" parser = argparse.ArgumentParser(description="An example",formatter_class

How to pass NULL to a optional parameter and it not be interpreted as a missing parameter

岁酱吖の 提交于 2021-01-07 02:47:04
问题 I have a SQL Server stored procedure that updates a large table. Because of its size, most parameters are optional. Most recommendations for setting the default of my optional parameters is to set them to NULL. However, my problem is that NULL is a legitimate update parameter value so I'm trying to figure out how to handle detect the difference between a NULL that is passed vs. a NULL that exists due to a missing optional parameter. One solution I've seen is to set the optional parameter's

How to pass NULL to a optional parameter and it not be interpreted as a missing parameter

邮差的信 提交于 2021-01-07 02:46:30
问题 I have a SQL Server stored procedure that updates a large table. Because of its size, most parameters are optional. Most recommendations for setting the default of my optional parameters is to set them to NULL. However, my problem is that NULL is a legitimate update parameter value so I'm trying to figure out how to handle detect the difference between a NULL that is passed vs. a NULL that exists due to a missing optional parameter. One solution I've seen is to set the optional parameter's

The default value of an optional parameter must be constant

不羁岁月 提交于 2020-08-10 23:09:56
问题 So Im creating this Event Tracker app and I have two screens which are the map and the events list. I am trying to get the place list to be equal to my places in my App state. Bare in mind that placeList is a modifiable list that I need to add places to this list. However I am getting a "The default value of an optional parameter must be constant" whenever I initialize this.places=PlaceMapState.placeList and I cant change it to a constant since i need it to update my list of places in the