What does square bracket [] mean in the below code?
Asked Answered
L

2

23

I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane. What does square bracket ([]) mean in the below code?

[Category("Custom Properties")]
        [WebPartStorage(Storage.Personal)]
        [FriendlyNameAttribute("Custom Color")]
        [Description("Select a color from the dropdown list.")]
        [Browsable(true)]
        [XmlElement(typeof(System.Drawing.KnownColor))]
        public System.Drawing.KnownColor MyColor
        {
            get
            {
                return _myColor;
            }
            set
            {
                _myColor = value;
            }
        }
Locoism answered 10/3, 2010 at 7:38 Comment(0)
O
15

As @Spencer Ruport said, they're attributes. They're used within .NET for declarative programming.

You can find information on each of these attributes at MSDN. However, you should know that the name of the attribute can be shortened. In your case, for example, Category is the short form of the class name CategoryAttribute and XmlElement is the short form of the class name XmlElementAttribute. When declaring attributes, the Attribute portion of the class name can be left out.

I've used most of these attributes in conjunction with the PropertyGrid control (see here for an example), although in your case, they are used for a Web Part property pane. The purpose is still the same. The attributes are used by the control to know how to display the property to the user. By using a combination of the various attributes that the control understands, it is possible to declaratively dictate this behavior.

I hope that helps a little bit, but Spencer is correct, you'll learn a lot more reading about attributes via Google than I can explain here.

Ortiz answered 10/3, 2010 at 8:2 Comment(1)
First link’s dead.Klaxon
C
15

They're called attributes.

Here's a quick example of how they can be used: http://www.codeproject.com/KB/cs/attributes.aspx

Convivial answered 10/3, 2010 at 7:38 Comment(3)
Could you explain little more?Locoism
I could but honestly there are so many well thought out articles you can quickly find on Google I'd recommend you look them over and come back with any more specific questions you have regarding them.Convivial
I short explanation would make this a much better answer.Uphemia
O
15

As @Spencer Ruport said, they're attributes. They're used within .NET for declarative programming.

You can find information on each of these attributes at MSDN. However, you should know that the name of the attribute can be shortened. In your case, for example, Category is the short form of the class name CategoryAttribute and XmlElement is the short form of the class name XmlElementAttribute. When declaring attributes, the Attribute portion of the class name can be left out.

I've used most of these attributes in conjunction with the PropertyGrid control (see here for an example), although in your case, they are used for a Web Part property pane. The purpose is still the same. The attributes are used by the control to know how to display the property to the user. By using a combination of the various attributes that the control understands, it is possible to declaratively dictate this behavior.

I hope that helps a little bit, but Spencer is correct, you'll learn a lot more reading about attributes via Google than I can explain here.

Ortiz answered 10/3, 2010 at 8:2 Comment(1)
First link’s dead.Klaxon

© 2022 - 2024 — McMap. All rights reserved.