Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the applicable rules for VB.NET default properties

Shulou Source: shulou.com Published: 2022-06-02 08:56:43 09月11日 Update

This article will explain in detail what are the applicable rules for the default attributes of VB.NET. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The emergence of the VB.NET programming language helps developers easily achieve many functions, and we can use it to help us improve our programming efficiency. In VB.NET, properties that accept parameters can be declared as the VB.NET default property of the class. The default property is the property that Microsoft Visual Basic .NET will use when a specific property is not named for the object. Default properties are useful because they allow you to simplify the source code by omitting common property names.

The properties that are most appropriate as default properties are those that accept parameters and you think will be the most commonly used. For example, the Item property is a good choice for the default property of a collection class because it is often used.

The following rules apply to the VB.NET default properties:

A type can have only one default property, including properties inherited from the base class. There is one exception to this rule. The default property defined in the base class can be hidden by another default property in the derived class.

If the default property in the base class is hidden by a non-default property in the derived class, the default property can still be accessed using the default property syntax.

The default property cannot be Shared or Private.

If an overloaded property is the VB.NET default property, all overloaded properties with the same name must also specify Default.

The default property must accept at least one parameter.

The following example declares a property that contains an array of strings as the default property of the class:

Class Class2

'Define a local variable

To store the property value.

Private PropertyValues As String ()

'Define the default property.

Default Public Property Prop1

(ByVal Index As Integer) As String

Get

Return PropertyValues (Index)

End Get

Set (ByVal Value As String)

If PropertyValues Is Nothing Then

'The array contains Nothing

When first accessed.

ReDim PropertyValues (0)

Else

'Re-dimension the array to

Hold the new element.

ReDim Preserve PropertyValues

(UBound (PropertyValues) + 1)

End If

PropertyValues (Index) = Value

End Set

End Property

End Class

Access VB.NET default properties

Default properties can be accessed using acronym syntax. For example, the following code snippet uses both standard and VB.NET default property syntax:

Dim C As New Class2 ()

'The first two lines of code

Access a property the standard way.

C.Prop1 (0) = "Value One"

'Property assignment.

MessageBox.Show (C.Prop1 (0))

'Property retrieval.

'The following two lines of

Code use default property syntax.

C (1) = "Value Two"

'Property assignment.

MessageBox.Show (C / 1)

'Property retrieval.

This is the end of this article on "what are the applicable rules for the default attributes of VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Attributes rules parameters articles syntax help commonly used more faceted programming good practical people code content functions at the same time characters strings objects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux macOS Docker MariaDB