Visual Basic 6 supported a wide variety of properties as default properties. This worked because we were required to use Let , Get , and Set when performing read and write operations against an object in VB6. However, Let , Get , and Set are not required or supported for VB .NET, so the compiler no longer has that codified cue. As a result a new cue was needed to differentiate a default property from an object assignment. The decision was made to support default properties only for properties that defined an indexer. The indexer is not require to be an integer, but the property must have an argument for us to use the Default keyword.
An example of a class with a default property is a class that has an Item property. Item properties are often indexed by integer values; hence it is the very presence of the indexer ”as in Foo(i) ”that differentiates an assignment to or from Foo and access of some indexed property of Foo . Listing 9.20 demonstrates two default properties for a strongly typed collection of Customer objects.
Listing 9.20 Two Default Properties of a Strongly Typed Collection
Default Public Property Item( _ ByVal index As Integer) As Customer Get Return CType(List(index), Customer) End Get Set(ByVal Value As Customer) List(index) = Value End Set End Property Default Public Property Item( _ ByVal Key As String) As Customer Get Return List(IndexOf(Key)) End Get Set(ByVal Value As Customer) List(IndexOf(Key)) = Value End Set End Property
The two default properties are part of a typed collection that inherits from System.Collections.CollectionBase . These two properties permit indexing of elements of the collection by an integer or string. You can download and explore DefaultDemo.sln for the complete listing.
There is a second context for default properties: the DefaultPropertyAttribute . This attribute indicates which property is the focus in the Properties window by default. Simply associate the DefaultPropertyAttribute , passing the name of the property as a string to the DefaultPropertyAttribute , with the control for which you want to define a default (or initial) property.
Because the literal verbiage is almost identical, it is important to establish whether you are conferring about an indexer ” Default Property ”or the initial property to edit ” DefaultPropertyAttribute ”when talking about default properties.