O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

DefaultMember Attribute

Class

System.Reflection.DefaultMemberAttribute

Applies to

Class, Struct, or Interface

Description

Defines the default member of a structure, class, or interface. The default member is the member executed by the Type object’s InvokeMember method when a null string is supplied as the method’s name argument.

The Visual Basic .NET Default keyword is ultimately translated by the Visual Basic .NET compiler into the <DefaultMember> attribute. Visual Basic, however, requires that default members be parameterized. The use of the default member then allows you to specify a particular array element without having to explicitly reference the member. For instance, if the Items property is the default member of NewObject1, the statement

NewObject1.Items(10) = "Sleeping bag"

is functionally identical to

NewObject(10) = "Sleeping bag"

This works in VB.NET because the latter code statement is translated by the compiler into a call to the InvokeMember method that looks something like the following:

Dim t As Type = GetType(NewClass1)
Dim iFlags As BindingFlags = BindingFlags.Public Or _
                             BindingFlags.Instance Or _
                             BindingFlags.SetProperty
Dim arr(  ) As Object = { 10, "Sleeping bag" }
t.InvokeMember("", iFlags, Nothing, NewObject, arr)

Because the <DefaultMember> attribute, unlike the Default keyword, does not have to refer to a parameterized property, you can use the <DefaultMember> attribute to define default members that are not parameterized. However, this does not allow ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required