Skip to content

Commit 1568dfc

Browse files
authored
System.ArrayTypeMismatchException F# snippets (#7514)
1 parent a515946 commit 1568dfc

File tree

9 files changed

+152
-0
lines changed

9 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// <Snippet1>
2+
open System
3+
4+
let copyArray (myArray: Array) (myArray1: Array) =
5+
let typeArray1 = myArray.GetType() |> string
6+
let typeArray2 = myArray1.GetType() |> string
7+
// Check whether the two arrays are of same type or not.
8+
if typeArray1 = typeArray2 then
9+
// Copy the values from one array to another.
10+
myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
11+
myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
12+
else
13+
// Throw an exception of type 'ArrayTypeMismatchException'.
14+
raise (ArrayTypeMismatchException())
15+
16+
try
17+
let myStringArray = [| "Jones"; "John" |]
18+
19+
let myIntArray = Array.zeroCreate<int> 2
20+
21+
copyArray myStringArray myIntArray
22+
23+
with :? ArrayTypeMismatchException as e ->
24+
printfn $"The Exception is: {e}"
25+
26+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="arraytypemismatch_constructor1.fs" />
8+
</ItemGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// <Snippet1>
2+
open System
3+
4+
let copyArray (myArray: Array) (myArray1: Array) =
5+
let typeArray1 = myArray.GetType() |> string
6+
let typeArray2 = myArray1.GetType() |> string
7+
// Check whether the two arrays are of same type or not.
8+
if typeArray1 = typeArray2 then
9+
// Copy the values from one array to another.
10+
myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
11+
myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
12+
else
13+
// Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
14+
raise (ArrayTypeMismatchException "The Source and destination arrays are not of same type.")
15+
16+
try
17+
let myStringArray = [| "Jones"; "John" |]
18+
19+
let myIntArray = Array.zeroCreate<int> 2
20+
21+
copyArray myStringArray myIntArray
22+
23+
with :? ArrayTypeMismatchException as e ->
24+
printfn $"The Exception is: {e}"
25+
26+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="arraytypemismatch_constructor2.fs" />
8+
</ItemGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <Snippet1>
2+
open System
3+
4+
let copyArray (myArray: Array) (myArray1: Array) =
5+
try
6+
// Copies the value of one array into another array.
7+
myArray.SetValue(myArray1.GetValue 0, 0)
8+
myArray.SetValue(myArray1.GetValue 1, 1)
9+
10+
with e ->
11+
// Throw an exception of with a message and innerexception.
12+
raise (ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e))
13+
14+
try
15+
let myStringArray = [| "Jones"; "John" |]
16+
let myIntArray = Array.zeroCreate<int> 2
17+
copyArray myStringArray myIntArray
18+
19+
with :? ArrayTypeMismatchException as e ->
20+
printfn $"The Exception Message is : {e.Message}"
21+
printfn $"The Inner exception is: {e.InnerException}"
22+
23+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="arraytypemismatch_constructor3.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//<Snippet1>
2+
open System
3+
4+
[<EntryPoint>]
5+
let main _ =
6+
let names = [| "Dog"; "Cat"; "Fish" |]
7+
let objs = box names :?> obj[]
8+
9+
try
10+
objs[2] <- "Mouse"
11+
12+
for animalName in objs do
13+
printfn $"{animalName}"
14+
15+
with :? ArrayTypeMismatchException ->
16+
// Not reached; "Mouse" is of the correct type.
17+
printfn "Exception Thrown."
18+
19+
try
20+
let obj = 13 :> obj
21+
objs[2] <- obj
22+
with :? ArrayTypeMismatchException ->
23+
// Always reached, 13 is not a string.
24+
printfn "New element is not of the correct type."
25+
26+
// Shadow objs as an array of objects instead of an array of strings.
27+
let objs: obj[] = [| "Turtle"; 12; 2.341 |]
28+
try
29+
for element in objs do
30+
printfn $"{element}"
31+
32+
with :? ArrayTypeMismatchException ->
33+
// ArrayTypeMismatchException is not thrown this time.
34+
printfn "Exception Thrown."
35+
36+
0
37+
//</Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="class1.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/ArrayTypeMismatchException.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
8383
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/CPP/class1.cpp" id="Snippet1":::
8484
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/CS/class1.cs" interactive="try-dotnet" id="Snippet1":::
85+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS/class1.fs" id="Snippet1":::
8586
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArrayTypeMismatch/VB/class1.vb" id="Snippet1":::
8687
8788
]]></format>
@@ -157,6 +158,7 @@
157158
158159
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/CPP/arraytypemismatch_constructor1.cpp" id="Snippet1":::
159160
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/CS/arraytypemismatch_constructor1.cs" id="Snippet1":::
161+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/FS/arraytypemismatch_constructor1.fs" id="Snippet1":::
160162
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/VB/arraytypemismatch_constructor1.vb" id="Snippet1":::
161163
162164
]]></format>
@@ -222,6 +224,7 @@
222224
223225
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/CPP/arraytypemismatch_constructor2.cpp" id="Snippet1":::
224226
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/CS/arraytypemismatch_constructor2.cs" id="Snippet1":::
227+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/FS/arraytypemismatch_constructor2.fs" id="Snippet1":::
225228
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/VB/arraytypemismatch_constructor2.vb" id="Snippet1":::
226229
227230
]]></format>
@@ -342,6 +345,7 @@
342345
343346
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/CPP/arraytypemismatch_constructor3.cpp" id="Snippet1":::
344347
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/CS/arraytypemismatch_constructor3.cs" id="Snippet1":::
348+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/FS/arraytypemismatch_constructor3.fs" id="Snippet1":::
345349
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/VB/arraytypemismatch_constructor3.vb" id="Snippet1":::
346350
347351
]]></format>

0 commit comments

Comments
 (0)