From 410ef6e38540c6c5bfb199da2d4dde9b51942e3b Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 26 Nov 2021 20:14:48 -0800 Subject: [PATCH] Add F# code snippets for ArgumentOutOfRangeException --- .../ArgumentOutOfRangeException/FS/FS.fsproj | 12 ++++ .../ArgumentOutOfRangeException/FS/program.fs | 20 ++++++ .../fs/BadSearch.fs | 33 +++++++++ .../fs/EmptyString1.fs | 29 ++++++++ .../fs/FindWords1.fs | 32 +++++++++ .../fs/NoElements.fs | 27 ++++++++ .../fs/NoElements2.fs | 26 +++++++ .../fs/NoFind1.fs | 21 ++++++ .../fs/NoFind2.fs | 22 ++++++ .../fs/OOR1.fs | 39 +++++++++++ .../fs/OOR2.fs | 31 +++++++++ .../fs/Race1.fs | 46 +++++++++++++ .../fs/Race2.fs | 67 +++++++++++++++++++ .../fs/argumentoutofrangeexception.fsproj | 22 ++++++ xml/System/ArgumentOutOfRangeException.xml | 19 ++++++ 15 files changed, 446 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/FS.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/program.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/FindWords1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/argumentoutofrangeexception.fsproj diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/FS.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/FS.fsproj new file mode 100644 index 00000000000..b179e0b5705 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/FS.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net6.0 + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/program.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/program.fs new file mode 100644 index 00000000000..94079bde365 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/program.fs @@ -0,0 +1,20 @@ +// +open System + +type Guest(fName: string, lName: string, age: int) = + let minimumRequiredAge = 21 + + do if age < minimumRequiredAge then + raise (ArgumentOutOfRangeException(nameof age, $"All guests must be {minimumRequiredAge}-years-old or older.")) + + member _.FirstName = fName + member _.LastName = lName + member _.GuestInfo() = $"{fName} {lName}, {age}" + +try + let guest1 = Guest("Ben", "Miller", 17); + printfn $"{guest1.GuestInfo()}" +with +| :? ArgumentOutOfRangeException as e -> + printfn $"Error: {e.Message}" +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs new file mode 100644 index 00000000000..4be5c58cf7d --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs @@ -0,0 +1,33 @@ +module BadSearch + +// +open System + +module StringSearcher = + let findEquals (s: string) value = + s.Equals(value, StringComparison.InvariantCulture) + +let list = ResizeArray() +list.AddRange [ "A"; "B"; "C" ] +// Get the index of the element whose value is "Z". +let index = list.FindIndex(StringSearcher.findEquals "Z") +try + printfn $"Index {index} contains '{list[index]}'" +with +| :? ArgumentOutOfRangeException as e -> + printfn $"{e.Message}" + +// The example displays the following output: +// Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') +// + +module Example2 = + let test () = + let list = new ResizeArray(); + list.AddRange [ "A"; "B"; "C" ] + // + // Get the index of the element whose value is "Z". + let index = list.FindIndex(StringSearcher.findEquals "Z") + if index >= 0 then + printfn $"'Z' is found at index {list[index]}" + // \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs new file mode 100644 index 00000000000..437a9fed98e --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs @@ -0,0 +1,29 @@ +module EmptyString1 + +// +open System + +let getFirstCharacter (s: string) = s[0] + +let words = [ "the"; "today"; "tomorrow"; " "; "" ] +for word in words do + printfn $"First character of '{word}': '{getFirstCharacter word}'" + +// The example displays the following output: +// First character of 'the': 't' +// First character of 'today': 't' +// First character of 'tomorrow': 't' +// First character of ' ': ' ' +// +// Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. +// at .$EmptyString1.main@() +// + +module StringLib = + // + let getFirstCharacter (s: string) = + if String.IsNullOrEmpty s then + '\u0000' + else + s[0] + // \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/FindWords1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/FindWords1.fs new file mode 100644 index 00000000000..0b0a6c46735 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/FindWords1.fs @@ -0,0 +1,32 @@ +module FindWords1 + +// + +let findWords (s: string) = + let mutable start, end' = 0, 0 + let delimiters = [| ' '; '.'; ','; ';'; ':'; '('; ')' |] + let words = ResizeArray() + while end' >= 0 do + end' <- s.IndexOfAny(delimiters, start) + if end' >= 0 then + if end' - start > 0 then + words.Add(s.Substring(start, end' - start)) + start <- end' + 1 + elif start < s.Length - 1 then + words.Add(s.Substring start) + words.ToArray() + +let sentence = "This is a simple, short sentence." +printfn $"Words in '{sentence}':" +for word in findWords sentence do + printfn $" '{word}'" + +// The example displays the following output: +// Words in 'This is a simple, short sentence.': +// 'This' +// 'is' +// 'a' +// 'simple' +// 'short' +// 'sentence' +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs new file mode 100644 index 00000000000..a3a97370462 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs @@ -0,0 +1,27 @@ +module NoElements + +// +open System + + +let list = ResizeArray() +printfn $"Number of items: {list.Count}" +try + printfn $"The first item: '{list[0]}'" +with +| :? ArgumentOutOfRangeException as e -> + printfn $"{e.Message}" + +// The example displays the following output: +// Number of items: 0 +// Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') +// + +module Example2 = + let test () = + let list = ResizeArray() + printfn $"Number of items: {list.Count}" + // + if list.Count > 0 then + printfn $"The first item: '{list[0]}'" + // diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs new file mode 100644 index 00000000000..13eb369c41c --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs @@ -0,0 +1,26 @@ +module NoElements2 + +// +let numbers = ResizeArray() +numbers.AddRange [ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 20 ] + +let squares = ResizeArray() +for ctr = 0 to numbers.Count - 1 do + squares[ctr] <- int (float numbers[ctr] ** 2) + +// The example displays the following output: +// Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') +// at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) +// at .$NoElements.main@() +// + +module Correction = + let test () = + let numbers = ResizeArray() + numbers.AddRange [| 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 20 |] + // + let squares = ResizeArray() + for ctr = 0 to numbers.Count - 1 do + squares.Add(int (float numbers[ctr] ** 2)) + // + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind1.fs new file mode 100644 index 00000000000..3d9edc68425 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind1.fs @@ -0,0 +1,21 @@ +module NoFind1 + +// +let getSecondWord (s: string) = + let pos = s.IndexOf " " + s.Substring(pos).Trim() + +let phrases = [ "ocean blue"; "concerned citizen"; "runOnPhrase" ] +for phrase in phrases do + printfn $"Second word is {getSecondWord phrase}" + +// The example displays the following output: +// Second word is blue +// Second word is citizen +// +// Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. (Parameter 'startIndex') +// at System.String.Substring(Int32 startIndex, Int32 length) +// at System.String.Substring(Int32 startIndex) +// at NoFind1.getSecondWord(String s) +// at .$NoFind1.main@() +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind2.fs new file mode 100644 index 00000000000..3d49390afcc --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind2.fs @@ -0,0 +1,22 @@ +module NoFind2 + +// +open System + +let getSecondWord (s: string) = + let pos = s.IndexOf " " + if pos >= 0 then + s.Substring(pos).Trim() + else + String.Empty + +let phrases = [ "ocean blue"; "concerned citizen"; "runOnPhrase" ] +for phrase in phrases do + let word = getSecondWord phrase + if not (String.IsNullOrEmpty word) then + printfn $"Second word is {word}" + +// The example displays the following output: +// Second word is blue +// Second word is citizen +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs new file mode 100644 index 00000000000..665e1cdde0a --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs @@ -0,0 +1,39 @@ +module OOR1 +// +open System + +let dimension1 = 10 +let dimension2 = -1 +try + let arr = Array.CreateInstance(typeof, dimension1, dimension2) + printfn "%A" arr +with +| :? ArgumentOutOfRangeException as e -> + if not (isNull e.ActualValue) then + printfn $"{e.ActualValue} is an invalid value for {e.ParamName}: " + printfn $"{e.Message}" + +// The example displays the following output: +// Non-negative number required. (Parameter 'length2') +// + +module Example2 = + let makeValid () = + // + let dimension1 = 10 + let dimension2 = 10 + let arr = Array.CreateInstance(typeof, dimension1, dimension2) + printfn "%A" arr + // + + let validate () = + let dimension1 = 10 + let dimension2 = 10 + // + if dimension1 < 0 || dimension2 < 0 then + printfn "Unable to create the array." + printfn "Specify non-negative values for the two dimensions." + else + let arr = Array.CreateInstance(typeof, dimension1, dimension2) + printfn "%A" arr + // \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs new file mode 100644 index 00000000000..fcb65eb986f --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs @@ -0,0 +1,31 @@ +module OOR2 + +// +open System + +let list = ResizeArray() +list.AddRange [ "A"; "B"; "C" ] +try + // Display the elements in the list by index. + for i = 0 to list.Count do + printfn $"Index {i}: {list[i]}" +with +| :? ArgumentOutOfRangeException as e -> + printfn $"{e.Message}" + +// The example displays the following output: +// Index 0: A +// Index 1: B +// Index 2: C +// Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') +// + +module Example2 = + let test () = + let list = ResizeArray() + list.AddRange [ "A"; "B"; "C" ] + // + // Display the elements in the list by index. + for i = 0 to list.Count - 1 do + printfn $"Index {i}: {list[i]}" + // diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race1.fs new file mode 100644 index 00000000000..6c2f0c2d905 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race1.fs @@ -0,0 +1,46 @@ +module Race1 + +// +open System.Threading + +type Continent = + { Name: string + Population: int + Area: decimal } + +let continents = ResizeArray() +let mutable msg = "" + +let names = + [ "Africa"; "Antarctica"; "Asia" + "Australia"; "Europe"; "North America" + "South America" ] + +let populateContinents obj = + let name = string obj + msg <- msg + $"Adding '{name}' to the list.\n" + // Sleep to simulate retrieving data. + Thread.Sleep 50 + let continent = + { Name = name + Population = 0 + Area = 0M } + continents.Add continent + +// Populate the list. +for name in names do + let th = Thread(ParameterizedThreadStart populateContinents) + th.Start name + +printfn $"{msg}\n" + +// Display the list. +for i = 0 to names.Length - 1 do + let continent = continents[i] + printfn $"{continent.Name}: Area: {continent.Population}, Population {continent.Area}" + +// The example displays output like the following: +// Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') +// at System.Collections.Generic.List`1.get_Item(Int32 index) +// at .$Race1.main@() +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race2.fs new file mode 100644 index 00000000000..3e90e1ed56f --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race2.fs @@ -0,0 +1,67 @@ +module Race2 + +// +open System.Collections.Concurrent +open System.Threading + +type Continent = + { Name: string + Population: int + Area: decimal } + +let continents = ConcurrentBag(); +let mutable msg = "" + +let names = + [ "Africa"; "Antarctica"; "Asia" + "Australia"; "Europe"; "North America" + "South America" ] + +let gate = new CountdownEvent(names.Length) + +let populateContinents obj = + let name = string obj + lock msg (fun () -> + msg <- msg + $"Adding '{name}' to the list.\n" ) + + // Sleep to simulate retrieving remaining data. + let continent = + { Name = name + Population = 0 + Area = 0M } + Thread.Sleep 25 + continents.Add continent + gate.Signal() |> ignore + +// Populate the list. +for name in names do + let th = Thread(ParameterizedThreadStart populateContinents) + th.Start name + +// Display the list. +gate.Wait(); +printfn $"{msg}\n" + +let arr = continents.ToArray(); +for i = 0 to names.Length - 1 do + let continent = arr[i] + printfn $"{continent.Name}: Area: {continent.Population}, Population {continent.Area}" + +// The example displays output like the following: +// Adding 'Africa' to the list. +// Adding 'Antarctica' to the list. +// Adding 'Asia' to the list. +// Adding 'Australia' to the list. +// Adding 'Europe' to the list. +// Adding 'North America' to the list. +// Adding 'South America' to the list. +// +// +// Africa: Area: 0, Population 0 +// Antarctica: Area: 0, Population 0 +// Asia: Area: 0, Population 0 +// Australia: Area: 0, Population 0 +// Europe: Area: 0, Population 0 +// North America: Area: 0, Population 0 +// South America: Area: 0, Population 0 +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/argumentoutofrangeexception.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/argumentoutofrangeexception.fsproj new file mode 100644 index 00000000000..8ec66061823 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/argumentoutofrangeexception.fsproj @@ -0,0 +1,22 @@ + + + + Exe + net6.0 + + + + + + + + + + + + + + + + + diff --git a/xml/System/ArgumentOutOfRangeException.xml b/xml/System/ArgumentOutOfRangeException.xml index 44a1b00cbf0..5177e18a1f8 100644 --- a/xml/System/ArgumentOutOfRangeException.xml +++ b/xml/System/ArgumentOutOfRangeException.xml @@ -83,41 +83,49 @@ The conditions in which an exception i 1. The collection has no members, and your code assumes that it does. The following example attempts to retrieve the first element of a collection that has no elements: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoElements.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoElements.vb" id="Snippet4"::: To prevent the exception, check whether the collection's `Count` property is greater than zero before attempting to retrieve any members, as the following code fragment does. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoElements.cs" id="Snippet5"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements.fs" id="Snippet5"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoElements.vb" id="Snippet5"::: 2. In some cases, the exception may occur because you are attempting to add a member to a collection by using an index that does not exist, rather than by calling the method, such as `Add`, that exists for this purpose. The following example attempts to add an element to a collection by using a non-existent index rather than calling the method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoElements2.cs" id="Snippet13"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs" id="Snippet13"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoElements2.vb" id="Snippet13"::: The following code fragment corrects this error: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoElements2.cs" id="Snippet14"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoElements2.fs" id="Snippet14"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoElements2.vb" id="Snippet14"::: 3. You're attempting to retrieve an item whose index is negative. This usually occurs because you've searched a collection for the index of a particular element and have erroneously assumed that the search is successful. In the following example, the call to the method fails to find a string equal to "Z" and so returns -1. However, this is an invalid index value. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/BadSearch.cs" interactive="try-dotnet" id="Snippet6"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs" interactive="try-dotnet" id="Snippet6"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/BadSearch.vb" id="Snippet6"::: To prevent the exception, check that the search is successful by making sure that the returned index is greater than or equal to zero before attempting to retrieve the item from the collection, as the following code fragment does. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/BadSearch.cs" id="Snippet7"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/BadSearch.fs" id="Snippet7"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/BadSearch.vb" id="Snippet7"::: 4. You're attempting to retrieve an element whose index is equal to the value of the collection's `Count` property, as the following example illustrates. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/OOR2.cs" interactive="try-dotnet" id="Snippet8"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs" id="Snippet8"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/OOR2.vb" id="Snippet8"::: Because collections in .NET use zero-based indexing, the first element of the collection is at index 0, and the last element is at index `Count` - 1. You can eliminate the error by ensuring that you access the last element at index `Count` - 1, as the following code does. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/OOR2.cs" id="Snippet9"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR2.fs" id="Snippet9"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/OOR2.vb" id="Snippet9"::: - You are attempting to perform a string operation by calling a string manipulation method, and the starting index does not exist in the string. @@ -129,11 +137,13 @@ The conditions in which an exception i 1. You are working with an empty string, or . Because its property returns 0, any attempt to manipulate it by index throws an exception. The following example, defines a `GetFirstCharacter` method that returns the first character of a string. If the string is empty, as the final string passed to the method is, the method throws an exception. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/EmptyString1.cs" id="Snippet15"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs" id="Snippet15"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/EmptyString1.vb" id="Snippet15"::: You can eliminate the exception by testing whether the string's is greater than zero or by calling the method to ensure that the string is not `null` or empty. The following code fragment does the latter. In this case, if the string is `null` or empty, the `GetFirstCharacter` method returns U+0000. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/EmptyString1.cs" id="Snippet16"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/EmptyString1.fs" id="Snippet16"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/EmptyString1.vb" id="Snippet16"::: 2. You're manipulating a string based on the position of a substring within that string, and you've failed to determine whether the substring was actually found. @@ -141,11 +151,13 @@ The conditions in which an exception i The following example extracts the second word of a two-word phrase. It throws an exception if the phrase consists of only one word, and therefore does not contain an embedded space character. This occurs because the call to the method returns -1 to indicate that the search failed, and this invalid value is then passed to the method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoFind1.cs" id="Snippet17"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind1.fs" id="Snippet17"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoFind1.vb" id="Snippet17"::: To eliminate the exception, validate the value returned by the string search method before calling the string manipulation method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/NoFind2.cs" interactive="try-dotnet" id="Snippet18"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/NoFind2.fs" id="Snippet18"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/NoFind2.vb" id="Snippet18"::: 3. You've attempted to extract a substring that is outside the range of the current string. @@ -169,6 +181,7 @@ The conditions in which an exception i The following example defines a `FindWords` method that uses the method to identify space characters and punctuation marks in a string and returns an array that contains the words found in the string. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/FindWords1.cs" interactive="try-dotnet" id="Snippet19"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/FindWords1.fs" id="Snippet19"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/FindWords1.vb" id="Snippet19"::: - You have passed a negative number to a method with an argument that requires only positive numbers and zero, or you have passed either a negative number or zero to a method with an argument that requires only positive numbers. @@ -176,16 +189,19 @@ The conditions in which an exception i For example, the method requires that you specify the number of elements in each dimension of a two-dimensional array; valid values for each dimension can range from 0 to . But because the dimension argument in the following example has a negative value, the method throws an exception. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/OOR1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/OOR1.vb" id="Snippet1"::: To correct the error, ensure that the value of the invalid argument is non-negative. You can do this by providing a valid value, as the following code fragment does. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/OOR1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/OOR1.vb" id="Snippet2"::: You can also validate the input and, if it is invalid, take some action. The following code fragment displays an error message instead of calling the method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/OOR1.cs" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/OOR1.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/OOR1.vb" id="Snippet3"::: - A race condition exists in an app that is multithreaded or has tasks that execute asynchronously and that updates an array or collection. @@ -193,6 +209,7 @@ The conditions in which an exception i The following example uses a object to populate a collection of `Continent` objects. It throws an if the example attempts to display the seven items in the collection before the collection is fully populated. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/Race1.cs" id="Snippet11"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race1.fs" id="Snippet11"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/Race1.vb" id="Snippet11"::: In this case, two resources are accessed from multiple threads: @@ -210,6 +227,7 @@ The conditions in which an exception i The following example addresses the and the other issues from the previous example. It replaces the object with a object to ensure that access to the collection is thread-safe, uses a object to ensure that the application thread continues only after other threads have executed, and uses a lock to ensure that only one thread can access the `msg` variable at a time. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/cs/Race2.cs" id="Snippet12"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/fs/Race2.fs" id="Snippet12"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.ArgumentOutOfRangeException/vb/Race2.vb" id="Snippet12"::: uses the HRESULT COR_E_ARGUMENTOUTOFRANGE, which has the value 0x80131502. @@ -221,6 +239,7 @@ For a list of initial property values for an instance of exception is thrown. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArgumentOutOfRangeException/CS/program.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentOutOfRangeException/FS/program.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArgumentOutOfRangeException/VB/program.vb" id="Snippet1"::: ]]>