Default parameter specifiers are not permitted
Asked Answered
B

4

9

I have the following code that gives the error

Default parameter specifiers are not permitted

How can this be fixed?

bool listSubscribe(string apikey,
                   string id, 
                   string email_address,
                   string [] merge_vars,
                   string email_type="html",
                   bool double_optin=false,
                   bool replace_interests=true,
                   bool send_welcome=false);

bool listUnsubscribe(string apikey, 
                     string id, 
                     string email_address, 
                     bool delete_menber=false,
                     bool send_goodbye=true,
                     bool send_notify=true);
Bilabial answered 19/10, 2011 at 13:57 Comment(5)
Are you targetting .NET 4.0? Visual Studio 2010?Heaume
which version of Visual Studio and which .NET framework are you using? are these methods inside a Web form or what kind of class?Aglitter
This might be worth reading: blogs.msdn.com/b/ericlippert/archive/2011/05/12/…Preston
I am using 3.5 .NET FrameworkBilabial
you need set language version in project properties, see https://mcmap.net/q/449463/-can-you-use-optional-parameters-in-code-targeting-net-3-5Hialeah
E
19

As per your error message, you can't do that in v3.5.

The work around is multiple constructors:

bool listUnsubscribe(string apikey, 
                     string id, 
                     string email_address) {
  return listUnsubscribe(apikey, id, email_address, false, true, true);
}

bool listUnsubscribe(string apikey, 
                     string id, 
                     string email_address, 
                     bool delete_menber,
                     bool send_goodbye,
                     bool send_notify) {
  return whatever;
}
Effuse answered 19/10, 2011 at 15:37 Comment(2)
Hi LarsTech I am doing this work in the interface so therefore it didn't allow to define the definition of the function, Could you recomeend any other solution for it.Bilabial
@user1003290 No, you won't get that to work in an interface. The interface would have to have all of the multiple constructors, and then in the class that consumes the interface, you would have to set the defaults. It can't be done from the interface alone.Effuse
T
9

I just now encountered this error and my project is also targeting 4.0 and not 3.5 or below.

I toggled it to 3.5 and then back to 4.0 and then the error went away. Hopefully these steps will work for you, or someone else.

Tiler answered 28/1, 2012 at 4:13 Comment(1)
To add to this, after several years, the toggling added a targetFramework="4.0" to the <compilation> node of the Web.config which could be manually added to resolve this as well.Caparison
E
7

The application/class library is not set to target .NET 4 Framework. Adjust in the project's settings page.

enter image description here

Eades answered 19/10, 2011 at 14:10 Comment(1)
OK p.campbell but whether there is not any alternative of it in .net framework 3.5Bilabial
G
5

Optional parameters are a feature of C# 4, not present in earlier versions. Since you're using .NET 3.5, you can't use optional parameters.

Either switch to .NET 4.0, or use overloaded methods instead.

Globefish answered 19/10, 2011 at 15:43 Comment(1)
-1, optional parameters can be used with .net 3.5, it need to set language version to default see https://mcmap.net/q/449463/-can-you-use-optional-parameters-in-code-targeting-net-3-5Hialeah

© 2022 - 2024 — McMap. All rights reserved.