Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.ArrayTypeMismatchException F# snippets #7514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <Snippet1>
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<int> 2

copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e ->
printfn $"The Exception is: {e}"

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="arraytypemismatch_constructor1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <Snippet1>
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<int> 2

copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e ->
printfn $"The Exception is: {e}"

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="arraytypemismatch_constructor2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <Snippet1>
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<int> 2
copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e ->
printfn $"The Exception Message is : {e.Message}"
printfn $"The Inner exception is: {e.InnerException}"

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="arraytypemismatch_constructor3.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//<Snippet1>
open System

[<EntryPoint>]
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
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="class1.fs" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions xml/System/ArrayTypeMismatchException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down