From 8764633d45cc7b20c4be83e10fbf078ee8b98931 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Tue, 21 Dec 2021 22:16:01 -0800 Subject: [PATCH] System.ArrayTypeMismatchException F# snippets --- .../FS/arraytypemismatch_constructor1.fs | 26 +++++++++++++ .../FS/fs.fsproj | 9 +++++ .../FS/arraytypemismatch_constructor2.fs | 26 +++++++++++++ .../FS/fs.fsproj | 9 +++++ .../FS/arraytypemismatch_constructor3.fs | 23 ++++++++++++ .../FS/fs.fsproj | 9 +++++ .../system.ArrayTypeMismatch/FS/class1.fs | 37 +++++++++++++++++++ .../system.ArrayTypeMismatch/FS/fs.fsproj | 9 +++++ xml/System/ArrayTypeMismatchException.xml | 4 ++ 9 files changed, 152 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/fs.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/fs.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/fs.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/fs.fsproj diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs new file mode 100644 index 00000000000..f7aa5c36207 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs @@ -0,0 +1,26 @@ +// +open System + +let copyArray (myArray: Array) (myArray1: Array) = + let typeArray1 = myArray.GetType() |> string + let typeArray2 = myArray1.GetType() |> string + // Check whether the two arrays are of same type or not. + if typeArray1 = typeArray2 then + // Copy the values from one array to another. + myArray.SetValue($"Name: {myArray1.GetValue 0}", 0) + myArray.SetValue($"Name: {myArray1.GetValue 1}", 1) + else + // Throw an exception of type 'ArrayTypeMismatchException'. + raise (ArrayTypeMismatchException()) + +try + let myStringArray = [| "Jones"; "John" |] + + let myIntArray = Array.zeroCreate 2 + + copyArray myStringArray myIntArray + +with :? ArrayTypeMismatchException as e -> + printfn $"The Exception is: {e}" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/fs.fsproj new file mode 100644 index 00000000000..16f2f903ae7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs new file mode 100644 index 00000000000..707128c35f2 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs @@ -0,0 +1,26 @@ +// +open System + +let copyArray (myArray: Array) (myArray1: Array) = + let typeArray1 = myArray.GetType() |> string + let typeArray2 = myArray1.GetType() |> string + // Check whether the two arrays are of same type or not. + if typeArray1 = typeArray2 then + // Copy the values from one array to another. + myArray.SetValue($"Name: {myArray1.GetValue 0}", 0) + myArray.SetValue($"Name: {myArray1.GetValue 1}", 1) + else + // Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter. + raise (ArrayTypeMismatchException "The Source and destination arrays are not of same type.") + +try + let myStringArray = [| "Jones"; "John" |] + + let myIntArray = Array.zeroCreate 2 + + copyArray myStringArray myIntArray + +with :? ArrayTypeMismatchException as e -> + printfn $"The Exception is: {e}" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/fs.fsproj new file mode 100644 index 00000000000..29580100f7f --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs new file mode 100644 index 00000000000..dd5f39af589 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs @@ -0,0 +1,23 @@ +// +open System + +let copyArray (myArray: Array) (myArray1: Array) = + try + // Copies the value of one array into another array. + myArray.SetValue(myArray1.GetValue 0, 0) + myArray.SetValue(myArray1.GetValue 1, 1) + + with e -> + // Throw an exception of with a message and innerexception. + raise (ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e)) + +try + let myStringArray = [| "Jones"; "John" |] + let myIntArray = Array.zeroCreate 2 + copyArray myStringArray myIntArray + +with :? ArrayTypeMismatchException as e -> + printfn $"The Exception Message is : {e.Message}" + printfn $"The Inner exception is: {e.InnerException}" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/fs.fsproj new file mode 100644 index 00000000000..12bf8d30b46 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs new file mode 100644 index 00000000000..06662e1b2fd --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs @@ -0,0 +1,37 @@ +// +open System + +[] +let main _ = + let names = [| "Dog"; "Cat"; "Fish" |] + let objs = box names :?> obj[] + + try + objs[2] <- "Mouse" + + for animalName in objs do + printfn $"{animalName}" + + with :? ArrayTypeMismatchException -> + // Not reached; "Mouse" is of the correct type. + printfn "Exception Thrown." + + try + let obj = 13 :> obj + objs[2] <- obj + with :? ArrayTypeMismatchException -> + // Always reached, 13 is not a string. + printfn "New element is not of the correct type." + + // Shadow objs as an array of objects instead of an array of strings. + let objs: obj[] = [| "Turtle"; 12; 2.341 |] + try + for element in objs do + printfn $"{element}" + + with :? ArrayTypeMismatchException -> + // ArrayTypeMismatchException is not thrown this time. + printfn "Exception Thrown." + + 0 +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/fs.fsproj new file mode 100644 index 00000000000..0d4b6596c65 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/xml/System/ArrayTypeMismatchException.xml b/xml/System/ArrayTypeMismatchException.xml index 4e4f0444fd3..6dd703489be 100644 --- a/xml/System/ArrayTypeMismatchException.xml +++ b/xml/System/ArrayTypeMismatchException.xml @@ -82,6 +82,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/CPP/class1.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/CS/class1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArrayTypeMismatch/VB/class1.vb" id="Snippet1"::: ]]> @@ -157,6 +158,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/CPP/arraytypemismatch_constructor1.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/CS/arraytypemismatch_constructor1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/VB/arraytypemismatch_constructor1.vb" id="Snippet1"::: ]]> @@ -222,6 +224,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/CPP/arraytypemismatch_constructor2.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/CS/arraytypemismatch_constructor2.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/VB/arraytypemismatch_constructor2.vb" id="Snippet1"::: ]]> @@ -342,6 +345,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/CPP/arraytypemismatch_constructor3.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/CS/arraytypemismatch_constructor3.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/VB/arraytypemismatch_constructor3.vb" id="Snippet1"::: ]]>