File tree 9 files changed +152
-0
lines changed
ArrayTypeMisMatch_Constructor1/FS
ArrayTypeMisMatch_Constructor2/FS
ArrayTypeMisMatch_Constructor3/FS
VS_Snippets_CLR_System/system.ArrayTypeMismatch/FS
9 files changed +152
-0
lines changed Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 82
82
83
83
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArrayTypeMismatch/CPP/class1.cpp" id="Snippet1":::
84
84
:::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":::
85
86
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArrayTypeMismatch/VB/class1.vb" id="Snippet1":::
86
87
87
88
]]> </format >
157
158
158
159
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/CPP/arraytypemismatch_constructor1.cpp" id="Snippet1":::
159
160
:::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":::
160
162
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor1/VB/arraytypemismatch_constructor1.vb" id="Snippet1":::
161
163
162
164
]]> </format >
222
224
223
225
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/CPP/arraytypemismatch_constructor2.cpp" id="Snippet1":::
224
226
:::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":::
225
228
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor2/VB/arraytypemismatch_constructor2.vb" id="Snippet1":::
226
229
227
230
]]> </format >
342
345
343
346
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/CPP/arraytypemismatch_constructor3.cpp" id="Snippet1":::
344
347
:::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":::
345
349
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArrayTypeMisMatch_Constructor3/VB/arraytypemismatch_constructor3.vb" id="Snippet1":::
346
350
347
351
]]> </format >
You can’t perform that action at this time.
0 commit comments