Compiler error "Default parameter specifiers are not permitted"
Asked Answered
C

2

11

Below is my code.

public class PItem
{
    public String content;
    public int count;
    public int fee;
    public int amount;
    public string description;

    // Default values
    public PItem(String _content = "", int _count = 0, int _fee = 0, string _description = "", int _amount = 0)
    {
        content = _content;
        count = _count < 0 ? 0 : _count;
        fee = _fee;
        description = _description;
        amount = _amount < 0 ? 0 : _amount;
    }
}

This is inside in a class. When I try to run a program it gives this error:

Default parameter specifiers are not permitted

How can I solve this error?

Confirm answered 18/6, 2011 at 19:28 Comment(6)
How do you know that you're using C# 4.0?Parmer
Where does this error occur? At time of compilation?Gat
yes akash.. on compiling i have got this errorConfirm
Gabe Sorry about C# 4.0... how can i solve my problmeConfirm
@hesamsalehnamadi: Now that you have solved your problem, you may want to consider giving back a little by editing or even deleting your question, since it's misleading (you are not using C# 4).Incommunicable
Your underscore convention for identifiers is exactly opposite from the usual meaning.Grad
S
27

The problem is that you cannot have optional parameters in C# version less than 4.
You can find more information on this here.

You can solve it like this:

public class PItem
{
  public String content;
  public int count;
  public int fee;
  public int amount;
  public String description;
  // default values
  public PItem(): this("", 0, 0, "", 0) {}
  public PItem(String _content): this (_content, 0, 0, "", 0) {}
  public PItem(String _content, int _count): this(_content, _count, 0, "", 0) {}
  public PItem(String _content, int _count, int _fee): this(_content, _count, _fee, "", 0) {}
  public PItem(String _content, int _count, int _fee, string _description): this(_content, _count, _fee, _description, 0) {}
  public PItem(String _content, int _count, int _fee, string _description, int _amount)
  {
      content = _content;
      count = _count < 0 ? 0 : _count;
      fee = _fee;
      description = _description;
      amount = _amount < 0 ? 0 : _amount;
  }
}
Slone answered 18/6, 2011 at 19:38 Comment(3)
-1: This is false information. The documentation (msdn.microsoft.com/en-us/library/dd264739.aspx) clearly states that "The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional."Incommunicable
@Jon: wording changed... Better?Slone
Better indeed. Although unfortunately this whole question is a fiasco.Incommunicable
T
4

If your project seems to be set as .NET 4.0 then change it to for example 3.5 and then change to the 4.0 again. I got this error when I included a class library project from my old solution solution to a new one when I wanted to have the project in my new software. Both solutions were .NET 4 but I got "default parameter specifiers are not permitted" error. I just did what I explained.

Toowoomba answered 10/4, 2012 at 22:56 Comment(2)
thanks! the same solution did the trick for me... but a really strange error!Lindblad
This also was the solution for me. Project was .NET 4.5 but was giving me this error on first build after I pulled it down from Github.Cordage

© 2022 - 2024 — McMap. All rights reserved.