Skip to content

Commit ada3b43

Browse files
zspitzBillWagner
andauthored
* Fix dotnet#24823 * Add vbproj; move standalone snippets to tuples.vb * Fix dotnet#27774 * Fix link typos * Fix link typos * Fix link typos Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
1 parent 7dc2ce0 commit ada3b43

File tree

8 files changed

+175
-139
lines changed

8 files changed

+175
-139
lines changed

docs/visual-basic/programming-guide/language-features/data-types/tuples.md

+21-11
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,31 @@ Starting with Visual Basic 2017, the Visual Basic language offers built-in suppo
1717

1818
You instantiate a tuple by enclosing its comma-delimited values in parentheses. Each of those values then becomes a field of the tuple. For example, the following code defines a triple (or 3-tuple) with a `Date` as its first value, a `String` as its second, and a `Boolean` as its third.
1919

20-
[!code-vb[Instantiate](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple1.vb#1)]
20+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="Instantiate":::
2121

2222
By default, the name of each field in a tuple consists of the string `Item` along with the field's one-based position in the tuple. For this 3-tuple, the `Date` field is `Item1`, the `String` field is `Item2`, and the `Boolean` field is `Item3`. The following example displays the values of fields of the tuple instantiated in the previous line of code
2323

24-
[!code-vb[Instantiate](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple1.vb#2)]
24+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="DefaultNames":::
2525

2626
The fields of a Visual Basic tuple are read-write; after you've instantiated a tuple, you can modify its values. The following example modifies two of the three fields of the tuple created in the previous example and displays the result.
2727

28-
[!code-vb[Instantiate](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple1.vb#3)]
28+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="Mutable":::
2929

3030
## Instantiating and using a named tuple
3131

3232
Rather than using default names for a tuple's fields, you can instantiate a *named tuple* by assigning your own names to the tuple's elements. The tuple's fields can then be accessed by their assigned names *or* by their default names. The following example instantiates the same 3-tuple as previously, except that it explicitly names the first field `EventDate`, the second `Name`, and the third `IsHoliday`. It then displays the field values, modifies them, and displays the field values again.
3333

34-
[!code-vb[Instantiate](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple1.vb#4)]
34+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="Named":::
35+
36+
You cam also specify the tuple names as part of the type declaration of a variable, field, or parameter:
37+
38+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="NamedDeclaration":::
39+
40+
or in the [return type of a method](#tuples-as-method-return-values).
41+
42+
This is particularly useful when providing tuples to a collection initializer; the tuple names can be provided as part of the collection's type declaration:
43+
44+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="NamedCollectionInitializer":::
3545

3646
## Inferred tuple element names
3747

@@ -102,19 +112,19 @@ Other conversions are not considered for assignments. Let's look at the kinds of
102112

103113
Consider these variables used in the following examples:
104114

105-
[!code-vb[Assign](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple3.vb#1)]
115+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="AssignmentsIDeclarations":::
106116

107117
The first two variables, `unnamed` and `anonymous`, do not have semantic names provided for the fields. Their field names are the default `Item1` and `Item2`. The last two variables, `named` and `differentName` have semantic field names. Note that these two tuples have different names for the fields.
108118

109119
All four of these tuples have the same number of fields (referred to as 'arity'), and the types of those fields are identical. Therefore, all of these assignments work:
110120

111-
[!code-vb[Assign](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple3.vb#2)]
121+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="Assignments":::
112122

113123
Notice that the names of the tuples are not assigned. The values of the fields are assigned following the order of the fields in the tuple.
114124

115125
Finally, notice that we can assign the `named` tuple to the `conversion` tuple, even though the first field of `named` is an `Integer`, and the first field of `conversion` is a `Long`. This assignment succeeds because converting an `Integer` to a `Long` is a widening conversion.
116126

117-
[!code-vb[Assign](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple3.vb#3)]
127+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="AssignmentImplicitConversion":::
118128

119129
Tuples with different numbers of fields are not assignable:
120130

@@ -138,15 +148,15 @@ A method can return only a single value. Frequently, though, you'd like a method
138148

139149
For example, the **TryParse** methods in .NET return a `Boolean` value that indicates whether the parsing operation succeeded. The result of the parsing operation is returned in a variable passed by reference to the method. Normally, a call to the a parsing method such as <xref:System.Int32.TryParse%2A?displayProperty=nameWithType> looks like the following:
140150

141-
[!code-vb[Return](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple-returns.vb#1)]
151+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuple-returns.vb" id="StandardMethodCall":::
142152

143153
We can return a tuple from the parsing operation if we wrap the call to the <xref:System.Int32.TryParse%2A?displayProperty=nameWithType> method in our own method. In the following example, `NumericLibrary.ParseInteger` calls the <xref:System.Int32.TryParse%2A?displayProperty=nameWithType> method and returns a named tuple with two elements.
144154

145-
[!code-vb[Return](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple-returns.vb#2)]
155+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuple-returns.vb" id="ParseIntegerReturnsTuple":::
146156

147157
You can then call the method with code like the following:
148158

149-
[!code-vb[Return](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple-returns.vb#3)]
159+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuple-returns.vb" id="MethodCallWithTuple":::
150160

151161
## Visual Basic tuples and tuples in the .NET Framework
152162

@@ -164,7 +174,7 @@ Extension methods in the <xref:System.TupleExtensions> class make it easy to con
164174

165175
The following example creates a tuple, converts it to a .NET **Tuple** object, and converts it back to a Visual Basic tuple. The example then compares this tuple with the original one to ensure that they are equal.
166176

167-
[!code-vb[Convert](../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple2.vb#1)]
177+
:::code language="vb" source="../../../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuples.vb" id="TupleValueTupleConversions":::
168178

169179
## See also
170180

docs/visual-basic/whats-new/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ Tuples are a lightweight data structure that most commonly is used to return mul
167167

168168
Visual Basic's support for tuples lets you quickly define a tuple, optionally assign semantic names to its values, and quickly retrieve its values. The following example wraps a call to the <xref:System.Int32.TryParse%2A> method and returns a tuple.
169169

170-
[!code-vb[Tuple](../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple-returns.vb#2)]
170+
:::code language="vb" source="../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuple-returns.vb" id="ParseIntegerReturnsTuple":::
171171

172172
You can then call the method and handle the returned tuple with code like the following.
173173

174-
[!code-vb[ReturnTuple](../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple-returns.vb#3)]
174+
:::code language="vb" source="../../../samples/snippets/visualbasic/programming-guide/language-features/data-types/tuples/tuple-returns.vb" id="MethodCallWithTuple":::
175175

176176
**Binary literals and digit separators**
177177

samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple1.vb

-47
This file was deleted.

samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple2.vb

-26
This file was deleted.

samples/snippets/visualbasic/programming-guide/language-features/data-types/tuple3.vb

-42
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
' <Snippet2>
1+
' <ParseIntegerReturnsTuple>
22
Imports System.Globalization
33

44
Public Module NumericLibrary
5-
Public Function ParseInteger(value As String) As (Success As Boolean, Number As Int32)
5+
Public Function ParseInteger(value As String) As (Success As Boolean, Number As Integer)
66
Dim number As Integer
7-
Return (Int32.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, number), number)
7+
Return (Integer.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, number), number)
88
End Function
99
End Module
10-
' </Snippet2>
10+
' </ParseIntegerReturnsTuple>
1111

12-
Module Module1
13-
Sub Main()
12+
Module TupleReturns
13+
Sub Run()
1414
StandardMethodCall()
1515
Console.WriteLine()
1616
MethodCallWithTuple()
1717
Console.ReadLine()
1818
End Sub
1919

2020
Private Sub StandardMethodCall()
21-
' <Snippet1>
21+
' <StandardMethodCall>
2222
Dim numericString As String = "123456"
2323
Dim number As Integer
24-
Dim result = Int32.TryParse(numericString, number)
24+
Dim result = Integer.TryParse(numericString, number)
2525
Console.WriteLine($"{If(result, $"Success: {number:N0}", "Failure")}")
2626
' Output: Success: 123,456
27-
' </Snippet1>
27+
' </StandardMethodCall>
2828
End Sub
2929

3030
Private Sub MethodCallWithTuple()
31-
' <Snippet3>
31+
' <MethodCallWithTuple>
3232
Dim numericString As String = "123,456"
3333
Dim result = ParseInteger(numericString)
3434
Console.WriteLine($"{If(result.Success, $"Success: {result.Number:N0}", "Failure")}")
3535
Console.ReadLine()
3636
' Output: Success: 123,456
37-
' </Snippet3>
37+
' </MethodCallWithTuple>
3838
End Sub
3939
End Module

0 commit comments

Comments
 (0)