Attribute not valid on declaration type
Asked Answered
S

3

8

I have the following code but I am getting the following compile errors:

Attribute 'WebPartStorage' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.

AND

Attribute 'FriendlyName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.

I have modified my code from the MSDN article: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint2003/dd584174(v=office.11). Does anyone have any idea what I am doing wrong that is causing this error?

  [Category("Custom Properties")]
    [DefaultValue(RegionEnum.None)]
    [WebPartStorage(Storage.Shared)]
    [FriendlyName("Region")]
    [Description("Select a value from the dropdown list.")]
    [Browsable(true)]
    protected RegionEnum _Region;
    public RegionEnum Region
    {
        get
        {
            return _Region;
        }
        set
        {
            _Region = value;
        }
    }
Shun answered 26/10, 2009 at 12:30 Comment(0)
B
13

You seem to have attached the attribute to the field; attributes always adhere to the next thing (in this case, the field). You should re-order so that they adhere to the property instead of the field.

BTW; protected fields are rarely a good idea (they should be private); but especially if the property is public: what is the point?

protected RegionEnum _Region;
[Category("Custom Properties")]
[DefaultValue(RegionEnum.None)]
[WebPartStorage(Storage.Shared)]
[FriendlyName("Region")]
[Description("Select a value from the dropdown list.")]
[Browsable(true)]
public RegionEnum Region
{
    get { return _Region; }
    set { _Region = value; }
}
Butters answered 26/10, 2009 at 12:34 Comment(0)
S
0

The message tells you, doesn't it? You are trying to set the attribute to a field, but it is only valid on indexers and properties.

protected RegionEnum _Region;

[Category("Custom Properties")]
[DefaultValue(RegionEnum.None)]
[Description("Select a value from the dropdown list.")]
[Browsable(true)]
[WebPartStorage(Storage.Shared)]
[FriendlyName("Region")]
public RegionEnum Region
{
    get
    {
        return _Region;
    }
    set
    {
        _Region = value;
    }
}
Stenophagous answered 26/10, 2009 at 12:33 Comment(0)
B
0

Hopefully you have using Microsoft.SharePoint.WebPartPages;, have you?

Boast answered 26/10, 2009 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.