Access Modifier Error: Cannot specify accessibility modifiers for both accessors of the property or indexer

Last Reply 11 months ago By dharmendr

Posted 11 months ago

Can anyone please explain what exactly wrong in following code. and what is correct way to write it?

 

Error: Cannot specify accessibility modifiers for both accessors of the property or indexer 

  public FileUpload ucFileUpload
    {
        protected get
        {
            return this.FileUpload1;
        }

        private set
        {
            this.FileUpload1 = value;
        }
    }
    public HiddenField ucHiddenContainer
    {
        protected get
        {
            return this.hfContainerNo;
        }

        private set
        {
            this.hfContainerNo = value;
        }
    }
Posted 11 months ago

Hi Gayatri,

As the error states that you Cannot specify accessibility modifiers for accessors of property. So change your code with below.

public FileUpload ucFileUpload
{
    get
    {
        return this.FileUpload1;
    }
    private set
    {
        this.FileUpload1 = value;
    }
}
public HiddenField ucHiddenContainer
{
    get
    {
        return this.hfContainerNo;
    }
    private set
    {
        this.hfContainerNo = value;
    }
}

Only one accessor can be modified.

For more details refer below link.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/restricting-accessor-accessibility