Skip to content

Commit 13ae32a

Browse files
CopilotBillWagner
andcommitted
Add parameterized property example and improve documentation links
Co-authored-by: BillWagner <[email protected]>
1 parent 3bbb320 commit 13ae32a

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

docs/visual-basic/language-reference/statements/property-statement.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslis
4444

4545
- `Default`
4646

47-
Optional. Specifies that this property is the default property for the class or structure on which it is defined. Default properties must accept parameters and can be set and retrieved without specifying the property name. If you declare the property as `Default`, you cannot use `Private` on the property or on either of its property procedures.
47+
Optional. Specifies that this property is the default property for the class or structure on which it is defined. Default properties must accept parameters and can be set and retrieved without specifying the property name. If you declare the property as `Default`, you cannot use `Private` on the property or on either of its property procedures. For examples and detailed guidance, see [How to: Declare and Call a Default Property in Visual Basic](../../programming-guide/language-features/procedures/how-to-declare-and-call-a-default-property.md).
4848

4949
- `accessmodifier`
5050

@@ -108,7 +108,7 @@ Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslis
108108

109109
- `parameterlist`
110110

111-
Optional. List of local variable names representing the parameters of this property, and possible additional parameters of the `Set` procedure. See [Parameter List](parameter-list.md).
111+
Optional. List of local variable names representing the parameters of this property, and possible additional parameters of the `Set` procedure. Parameterized properties are often used to create indexers or default properties that allow collection-like access. See [Parameter List](parameter-list.md) and [How to: Declare and Call a Default Property in Visual Basic](../../programming-guide/language-features/procedures/how-to-declare-and-call-a-default-property.md).
112112

113113
- `returntype`
114114

@@ -163,7 +163,7 @@ The `Property` statement introduces the declaration of a property. A property ca
163163

164164
You can use `Property` only at class level. This means the *declaration context* for a property must be a class, structure, module, or interface, and cannot be a source file, namespace, procedure, or block. For more information, see [Declaration Contexts and Default Access Levels](declaration-contexts-and-default-access-levels.md).
165165

166-
By default, properties use public access. You can adjust a property's access level with an access modifier on the `Property` statement, and you can optionally adjust one of its property procedures to a more restrictive access level.
166+
By default, properties use public access. You can adjust a property's access level with an access modifier on the `Property` statement, and you can optionally adjust one of its property procedures to a more restrictive access level. For detailed examples of mixed access levels, see [How to: Declare a Property with Mixed Access Levels](../../programming-guide/language-features/procedures/how-to-declare-a-property-with-mixed-access-levels.md).
167167

168168
Visual Basic passes a parameter to the `Set` procedure during property assignments. If you do not supply a parameter for `Set`, the integrated development environment (IDE) uses an implicit parameter named `value`. This parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the `Get` procedure is called.
169169

@@ -205,11 +205,31 @@ The following example declares a property in a class.
205205

206206
[!code-vb[VbVbalrStatements#51](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#51)]
207207

208+
### Parameterized Properties
209+
210+
The following example shows how to create a parameterized property, also called an indexer, which allows array-like access to a collection:
211+
212+
[!code-vb[VbVbalrStatements#52](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#52)]
213+
214+
For comprehensive examples of property usage, including automatic implementation, mixed access levels, and validation scenarios, see [Property Procedures](../../programming-guide/language-features/procedures/property-procedures.md).
215+
208216
## See also
209217

218+
### Property Types and Features
219+
210220
- [Automatically implemented properties](../../programming-guide/language-features/procedures/auto-implemented-properties.md)
211-
- [Objects and Classes](../../programming-guide/language-features/objects-and-classes/index.md)
221+
- [Property Procedures](../../programming-guide/language-features/procedures/property-procedures.md)
222+
- [How to: Create a Property](../../programming-guide/language-features/procedures/how-to-create-a-property.md)
223+
224+
### Advanced Property Scenarios
225+
226+
- [How to: Declare and Call a Default Property in Visual Basic](../../programming-guide/language-features/procedures/how-to-declare-and-call-a-default-property.md)
227+
- [How to: Declare a Property with Mixed Access Levels](../../programming-guide/language-features/procedures/how-to-declare-a-property-with-mixed-access-levels.md)
228+
229+
### Related Statements and Concepts
230+
212231
- [Get Statement](get-statement.md)
213232
- [Set Statement](set-statement.md)
214233
- [Parameter List](parameter-list.md)
215234
- [Default](../modifiers/default.md)
235+
- [Objects and Classes](../../programming-guide/language-features/objects-and-classes/index.md)

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,48 @@ Public Class Class1
598598
'</Snippet51>
599599

600600

601+
'********************************************************************
602+
'<Snippet52>
603+
Class SampleCollection
604+
' Define a local collection to store strings.
605+
Private items As New List(Of String)
606+
607+
' Define a parameterized property (indexer) for the collection.
608+
Default Public Property Item(ByVal index As Integer) As String
609+
Get
610+
' Return the item at the specified index.
611+
If index >= 0 AndAlso index < items.Count Then
612+
Return items(index)
613+
Else
614+
Return Nothing
615+
End If
616+
End Get
617+
Set(ByVal value As String)
618+
' Set the item at the specified index.
619+
If index >= 0 AndAlso index < items.Count Then
620+
items(index) = value
621+
ElseIf index = items.Count Then
622+
' Allow adding new items at the end.
623+
items.Add(value)
624+
End If
625+
End Set
626+
End Property
627+
628+
' Add a Count property for convenience.
629+
Public ReadOnly Property Count As Integer
630+
Get
631+
Return items.Count
632+
End Get
633+
End Property
634+
635+
' Add method to add items.
636+
Public Sub Add(ByVal item As String)
637+
items.Add(item)
638+
End Sub
639+
End Class
640+
'</Snippet52>
641+
642+
601643
'********************************************************************
602644
'<Snippet44>
603645
Public Structure abc

0 commit comments

Comments
 (0)