VB.NET default properties

zhaozj2021-02-16  56

The properties of the acceptance parameters can be declared as the default properties of the class. "Default Properties" is the properties that Microsoft Visual Basic .NET will use when naming a specific property to an object. Because the default properties make the source code more streamlined by omitting the common property name, the default properties is very useful. The most suitable as default properties is those acceptable parameters and what you think will be the most commonly used. For example, the item attribute is a good choice for a collection class default attribute because it is often used. The following rules apply to the default attribute: one type can only have a default property, including the properties inherited from the base class. This rule has an exception. The default attribute defined in the base class can be hidden by another default attribute in the derived class. If the default attributes in the base class are hidden in the derived attribute in the derived class, the default properties can be accessed using the default attribute syntax. The default attribute cannot be Shared or Private. If an overload attribute is the default property, all the overload properties of the same name must also specify default. The default attribute must accept at least one parameter. Example The following example attribute declaration contains an array of strings for the default attributes of the classes: 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 StringGetReturn PropertyValues ​​(Index) End GetSet (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 ​​(PropertyValues) 1) end ifpropertyvalues ​​(index) = ValueEnd Sendend PropertyEnd Class Access Default Property Access the default properties using an abnormal syntax. For example, the following code snippet uses standard and default attribute syntax: DIM C as new class2 () 'the first two lines of code access a property the standard war.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.

转载请注明原文地址:https://www.9cbs.com/read-21190.html

New Post(0)