File tree Expand file tree Collapse file tree 15 files changed +446
-0
lines changed
VS_Snippets_CLR/ArgumentOutOfRangeException/FS
VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs Expand file tree Collapse file tree 15 files changed +446
-0
lines changed Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+
3
+ <PropertyGroup >
4
+ <OutputType >Exe</OutputType >
5
+ <TargetFramework >net6.0</TargetFramework >
6
+ </PropertyGroup >
7
+
8
+ <ItemGroup >
9
+ <Compile Include =" program.fs" />
10
+ </ItemGroup >
11
+
12
+ </Project >
Original file line number Diff line number Diff line change
1
+ // <Snippet1>
2
+ open System
3
+
4
+ type Guest ( fName : string , lName : string , age : int ) =
5
+ let minimumRequiredAge = 21
6
+
7
+ do if age < minimumRequiredAge then
8
+ raise ( ArgumentOutOfRangeException( nameof age, $" All guests must be {minimumRequiredAge}-years-old or older." ))
9
+
10
+ member _.FirstName = fName
11
+ member _.LastName = lName
12
+ member _.GuestInfo () = $" {fName} {lName}, {age}"
13
+
14
+ try
15
+ let guest1 = Guest( " Ben" , " Miller" , 17 );
16
+ printfn $" {guest1.GuestInfo()}"
17
+ with
18
+ | :? ArgumentOutOfRangeException as e ->
19
+ printfn $" Error: {e.Message}"
20
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ module BadSearch
2
+
3
+ // <Snippet6>
4
+ open System
5
+
6
+ module StringSearcher =
7
+ let findEquals ( s : string ) value =
8
+ s.Equals( value, StringComparison.InvariantCulture)
9
+
10
+ let list = ResizeArray< string>()
11
+ list.AddRange [ " A" ; " B" ; " C" ]
12
+ // Get the index of the element whose value is "Z".
13
+ let index = list.FindIndex( StringSearcher.findEquals " Z" )
14
+ try
15
+ printfn $" Index {index} contains '{list[index]}'"
16
+ with
17
+ | :? ArgumentOutOfRangeException as e ->
18
+ printfn $" {e.Message}"
19
+
20
+ // The example displays the following output:
21
+ // Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
22
+ // </Snippet6>
23
+
24
+ module Example2 =
25
+ let test () =
26
+ let list = new ResizeArray< string>();
27
+ list.AddRange [ " A" ; " B" ; " C" ]
28
+ // <Snippet7>
29
+ // Get the index of the element whose value is "Z".
30
+ let index = list.FindIndex( StringSearcher.findEquals " Z" )
31
+ if index >= 0 then
32
+ printfn $" 'Z' is found at index {list[index]}"
33
+ // </Snippet7>
Original file line number Diff line number Diff line change
1
+ module EmptyString1
2
+
3
+ // <Snippet15>
4
+ open System
5
+
6
+ let getFirstCharacter ( s : string ) = s[ 0 ]
7
+
8
+ let words = [ " the" ; " today" ; " tomorrow" ; " " ; " " ]
9
+ for word in words do
10
+ printfn $" First character of '{word}': '{getFirstCharacter word}'"
11
+
12
+ // The example displays the following output:
13
+ // First character of 'the': 't'
14
+ // First character of 'today': 't'
15
+ // First character of 'tomorrow': 't'
16
+ // First character of ' ': ' '
17
+ //
18
+ // Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
19
+ // at <StartupCode$argumentoutofrangeexception>.$EmptyString1.main@()
20
+ // </Snippet15>
21
+
22
+ module StringLib =
23
+ // <Snippet16>
24
+ let getFirstCharacter ( s : string ) =
25
+ if String.IsNullOrEmpty s then
26
+ '\u0000'
27
+ else
28
+ s[ 0 ]
29
+ // </Snippet16>
Original file line number Diff line number Diff line change
1
+ module FindWords1
2
+
3
+ // <Snippet19>
4
+
5
+ let findWords ( s : string ) =
6
+ let mutable start , end' = 0 , 0
7
+ let delimiters = [| ' ' ; '.' ; ',' ; ';' ; ':' ; '(' ; ')' |]
8
+ let words = ResizeArray< string>()
9
+ while end' >= 0 do
10
+ end' <- s.IndexOfAny( delimiters, start)
11
+ if end' >= 0 then
12
+ if end' - start > 0 then
13
+ words.Add( s.Substring( start, end' - start))
14
+ start <- end' + 1
15
+ elif start < s.Length - 1 then
16
+ words.Add( s.Substring start)
17
+ words.ToArray()
18
+
19
+ let sentence = " This is a simple, short sentence."
20
+ printfn $" Words in '{sentence}':"
21
+ for word in findWords sentence do
22
+ printfn $" '{word}'"
23
+
24
+ // The example displays the following output:
25
+ // Words in 'This is a simple, short sentence.':
26
+ // 'This'
27
+ // 'is'
28
+ // 'a'
29
+ // 'simple'
30
+ // 'short'
31
+ // 'sentence'
32
+ // </Snippet19>
Original file line number Diff line number Diff line change
1
+ module NoElements
2
+
3
+ // <Snippet4>
4
+ open System
5
+
6
+
7
+ let list = ResizeArray< string>()
8
+ printfn $" Number of items: {list.Count}"
9
+ try
10
+ printfn $" The first item: '{list[0]}'"
11
+ with
12
+ | :? ArgumentOutOfRangeException as e ->
13
+ printfn $" {e.Message}"
14
+
15
+ // The example displays the following output:
16
+ // Number of items: 0
17
+ // Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
18
+ // </Snippet4>
19
+
20
+ module Example2 =
21
+ let test () =
22
+ let list = ResizeArray< string>()
23
+ printfn $" Number of items: {list.Count}"
24
+ // <Snippet5>
25
+ if list.Count > 0 then
26
+ printfn $" The first item: '{list[0]}'"
27
+ // </Snippet5>
Original file line number Diff line number Diff line change
1
+ module NoElements2
2
+
3
+ // <Snippet13>
4
+ let numbers = ResizeArray< int>()
5
+ numbers.AddRange [ 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 20 ]
6
+
7
+ let squares = ResizeArray< int>()
8
+ for ctr = 0 to numbers.Count - 1 do
9
+ squares[ ctr] <- int ( float numbers[ ctr] ** 2 )
10
+
11
+ // The example displays the following output:
12
+ // Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
13
+ // at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
14
+ // at <StartupCode$argumentoutofrangeexception>.$NoElements.main@()
15
+ // </Snippet13>
16
+
17
+ module Correction =
18
+ let test () =
19
+ let numbers = ResizeArray< int>()
20
+ numbers.AddRange [| 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 20 |]
21
+ // <Snippet14>
22
+ let squares = ResizeArray< int>()
23
+ for ctr = 0 to numbers.Count - 1 do
24
+ squares.Add( int ( float numbers[ ctr] ** 2 ))
25
+ // </Snippet14>
26
+
Original file line number Diff line number Diff line change
1
+ module NoFind1
2
+
3
+ // <Snippet17>
4
+ let getSecondWord ( s : string ) =
5
+ let pos = s.IndexOf " "
6
+ s.Substring( pos) .Trim()
7
+
8
+ let phrases = [ " ocean blue" ; " concerned citizen" ; " runOnPhrase" ]
9
+ for phrase in phrases do
10
+ printfn $" Second word is {getSecondWord phrase}"
11
+
12
+ // The example displays the following output:
13
+ // Second word is blue
14
+ // Second word is citizen
15
+ //
16
+ // Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. (Parameter 'startIndex')
17
+ // at System.String.Substring(Int32 startIndex, Int32 length)
18
+ // at System.String.Substring(Int32 startIndex)
19
+ // at NoFind1.getSecondWord(String s)
20
+ // at <StartupCode$argumentoutofrangeexception>.$NoFind1.main@()
21
+ // </Snippet17>
Original file line number Diff line number Diff line change
1
+ module NoFind2
2
+
3
+ // <Snippet18>
4
+ open System
5
+
6
+ let getSecondWord ( s : string ) =
7
+ let pos = s.IndexOf " "
8
+ if pos >= 0 then
9
+ s.Substring( pos) .Trim()
10
+ else
11
+ String.Empty
12
+
13
+ let phrases = [ " ocean blue" ; " concerned citizen" ; " runOnPhrase" ]
14
+ for phrase in phrases do
15
+ let word = getSecondWord phrase
16
+ if not ( String.IsNullOrEmpty word) then
17
+ printfn $" Second word is {word}"
18
+
19
+ // The example displays the following output:
20
+ // Second word is blue
21
+ // Second word is citizen
22
+ // </Snippet18>
Original file line number Diff line number Diff line change
1
+ module OOR1
2
+ // <Snippet1>
3
+ open System
4
+
5
+ let dimension1 = 10
6
+ let dimension2 = - 1
7
+ try
8
+ let arr = Array.CreateInstance( typeof< string>, dimension1, dimension2)
9
+ printfn " %A " arr
10
+ with
11
+ | :? ArgumentOutOfRangeException as e ->
12
+ if not ( isNull e.ActualValue) then
13
+ printfn $" {e.ActualValue} is an invalid value for {e.ParamName}: "
14
+ printfn $" {e.Message}"
15
+
16
+ // The example displays the following output:
17
+ // Non-negative number required. (Parameter 'length2')
18
+ // </Snippet1>
19
+
20
+ module Example2 =
21
+ let makeValid () =
22
+ // <Snippet2>
23
+ let dimension1 = 10
24
+ let dimension2 = 10
25
+ let arr = Array.CreateInstance( typeof< string>, dimension1, dimension2)
26
+ printfn " %A " arr
27
+ // </Snippet2>
28
+
29
+ let validate () =
30
+ let dimension1 = 10
31
+ let dimension2 = 10
32
+ // <Snippet3>
33
+ if dimension1 < 0 || dimension2 < 0 then
34
+ printfn " Unable to create the array."
35
+ printfn " Specify non-negative values for the two dimensions."
36
+ else
37
+ let arr = Array.CreateInstance( typeof< string>, dimension1, dimension2)
38
+ printfn " %A " arr
39
+ // </Snippet3>
Original file line number Diff line number Diff line change
1
+ module OOR2
2
+
3
+ // <Snippet8>
4
+ open System
5
+
6
+ let list = ResizeArray< string>()
7
+ list.AddRange [ " A" ; " B" ; " C" ]
8
+ try
9
+ // Display the elements in the list by index.
10
+ for i = 0 to list.Count do
11
+ printfn $" Index {i}: {list[i]}"
12
+ with
13
+ | :? ArgumentOutOfRangeException as e ->
14
+ printfn $" {e.Message}"
15
+
16
+ // The example displays the following output:
17
+ // Index 0: A
18
+ // Index 1: B
19
+ // Index 2: C
20
+ // Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
21
+ // </Snippet8>
22
+
23
+ module Example2 =
24
+ let test () =
25
+ let list = ResizeArray< string>()
26
+ list.AddRange [ " A" ; " B" ; " C" ]
27
+ // <Snippet9>
28
+ // Display the elements in the list by index.
29
+ for i = 0 to list.Count - 1 do
30
+ printfn $" Index {i}: {list[i]}"
31
+ // </Snippet9>
Original file line number Diff line number Diff line change
1
+ module Race1
2
+
3
+ // <Snippet11>
4
+ open System.Threading
5
+
6
+ type Continent =
7
+ { Name: string
8
+ Population: int
9
+ Area: decimal }
10
+
11
+ let continents = ResizeArray< Continent>()
12
+ let mutable msg = " "
13
+
14
+ let names =
15
+ [ " Africa" ; " Antarctica" ; " Asia"
16
+ " Australia" ; " Europe" ; " North America"
17
+ " South America" ]
18
+
19
+ let populateContinents obj =
20
+ let name = string obj
21
+ msg <- msg + $" Adding '{name}' to the list.\n "
22
+ // Sleep to simulate retrieving data.
23
+ Thread.Sleep 50
24
+ let continent =
25
+ { Name = name
26
+ Population = 0
27
+ Area = 0 M }
28
+ continents.Add continent
29
+
30
+ // Populate the list.
31
+ for name in names do
32
+ let th = Thread( ParameterizedThreadStart populateContinents)
33
+ th.Start name
34
+
35
+ printfn $" {msg}\n "
36
+
37
+ // Display the list.
38
+ for i = 0 to names.Length - 1 do
39
+ let continent = continents[ i]
40
+ printfn $" {continent.Name}: Area: {continent.Population}, Population {continent.Area}"
41
+
42
+ // The example displays output like the following:
43
+ // Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
44
+ // at System.Collections.Generic.List`1.get_Item(Int32 index)
45
+ // at <StartupCode$argumentoutofrangeexception>.$Race1.main@()
46
+ // </Snippet11>
You can’t perform that action at this time.
0 commit comments