Skip to content

Commit 8606ed4

Browse files
authored
InvalidCastException F# snippets (#7772)
1 parent 1cb07a2 commit 8606ed4

File tree

7 files changed

+110
-0
lines changed

7 files changed

+110
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Interface1
2+
3+
// <Snippet3>
4+
open System
5+
open System.Globalization
6+
7+
let culture = CultureInfo.InvariantCulture
8+
let provider: IFormatProvider = culture
9+
10+
let dt = provider :?> DateTimeFormatInfo
11+
12+
// The example displays the following output:
13+
// Unhandled Exception: System.InvalidCastException:
14+
// Unable to cast object of type //System.Globalization.CultureInfo// to
15+
// type //System.Globalization.DateTimeFormatInfo//.
16+
// at Example.main()
17+
// </Snippet3>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module ToString1
2+
3+
// <Snippet4>
4+
let value: obj = 12
5+
// Cast throws an InvalidCastException exception.
6+
let s = value :?> string
7+
// </Snippet4>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module ToString2
2+
3+
// <Snippet5>
4+
let value: obj = 12
5+
let s = value.ToString()
6+
printfn $"{s}"
7+
// The example displays the following output:
8+
// 12
9+
// </Snippet5>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module basetoderived1
2+
3+
// <Snippet1>
4+
open System
5+
6+
type Person() =
7+
member val Name = String.Empty with get, set
8+
9+
type PersonWithId() =
10+
inherit Person()
11+
member val Id = String.Empty with get, set
12+
13+
14+
let p = Person()
15+
p.Name <- "John"
16+
try
17+
let pid = p :?> PersonWithId
18+
printfn "Conversion succeeded."
19+
with :? InvalidCastException ->
20+
printfn "Conversion failed."
21+
22+
let pid1 = PersonWithId()
23+
pid1.Name <- "John"
24+
pid1.Id <- "246"
25+
let p1: Person = pid1
26+
try
27+
let pid1a = p1 :?> PersonWithId
28+
printfn "Conversion succeeded."
29+
with :? InvalidCastException ->
30+
printfn "Conversion failed."
31+
32+
// The example displays the following output:
33+
// Conversion failed.
34+
// Conversion succeeded.
35+
// </Snippet1>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="iconvertible1.fs" />
9+
<Compile Include="basetoderived1.fs" />
10+
<Compile Include="Interface1.fs" />
11+
<Compile Include="ToString1.fs" />
12+
<Compile Include="ToString2.fs" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module iconvertible1
2+
3+
// <Snippet2>
4+
open System
5+
6+
let flag = true
7+
try
8+
let conv: IConvertible = flag
9+
let ch = conv.ToChar null
10+
printfn "Conversion succeeded."
11+
with :? InvalidCastException ->
12+
printfn "Cannot convert a Boolean to a Char."
13+
14+
try
15+
let ch = Convert.ToChar flag
16+
printfn "Conversion succeeded."
17+
with :? InvalidCastException ->
18+
printfn "Cannot convert a Boolean to a Char."
19+
20+
// The example displays the following output:
21+
// Cannot convert a Boolean to a Char.
22+
// Cannot convert a Boolean to a Char.
23+
// </Snippet2>

xml/System/InvalidCastException.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ For a list of initial property values for an instance of <xref:System.InvalidCas
100100
You directly or indirectly call a primitive type's <xref:System.IConvertible> implementation that does not support a particular conversion. For example, trying to convert a <xref:System.Boolean> value to a <xref:System.Char> or a <xref:System.DateTime> value to an <xref:System.Int32> throws an <xref:System.InvalidCastException> exception. The following example calls both the <xref:System.Boolean.System%23IConvertible%23ToChar%2A?displayProperty=nameWithType> and <xref:System.Convert.ToChar%28System.Boolean%29?displayProperty=nameWithType> methods to convert a <xref:System.Boolean> value to a <xref:System.Char>. In both cases, the method call throws an <xref:System.InvalidCastException> exception.
101101
102102
:::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/iconvertible1.cs" interactive="try-dotnet" id="Snippet2":::
103+
:::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs" id="Snippet2":::
103104
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidcastexception/vb/iconvertible1.vb" id="Snippet2":::
104105
105106
Because the conversion is not supported, there is no workaround.
@@ -123,6 +124,7 @@ For a list of initial property values for an instance of <xref:System.InvalidCas
123124
You are downcasting, that is, trying to convert an instance of a base type to one of its derived types. In the following example, trying to convert a `Person` object to a `PersonWithID` object fails.
124125
125126
:::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/basetoderived1.cs" interactive="try-dotnet" id="Snippet1":::
127+
:::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/basetoderived1.fs" id="Snippet1":::
126128
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidcastexception/vb/basetoderived1.vb" id="Snippet1":::
127129
128130
As the example shows, the downcast succeeds only if the `Person` object was created by an upcast from a `PersonWithId` object to a `Person` object, or if the `Person` object is `null`.
@@ -132,6 +134,7 @@ For a list of initial property values for an instance of <xref:System.InvalidCas
132134
You are attempting to convert an interface object to a type that implements that interface, but the target type is not the same type or a base class of the type from which the interface object was originally derived. The following example throws an <xref:System.InvalidCastException> exception when it attempts to convert an <xref:System.IFormatProvider> object to a <xref:System.Globalization.DateTimeFormatInfo> object. The conversion fails because although the <xref:System.Globalization.DateTimeFormatInfo> class implements the <xref:System.IFormatProvider> interface, the <xref:System.Globalization.DateTimeFormatInfo> object is not related to the <xref:System.Globalization.CultureInfo> class from which the interface object was derived.
133135
134136
:::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/Interface1.cs" id="Snippet3":::
137+
:::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs" id="Snippet3":::
135138
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidcastexception/vb/Interface1.vb" id="Snippet3":::
136139
137140
As the exception message indicates, the conversion would succeed only if the interface object is converted back to an instance of the original type, in this case a <xref:System.Globalization.CultureInfo>. The conversion would also succeed if the interface object is converted to an instance of a base type of the original type.
@@ -141,13 +144,15 @@ For a list of initial property values for an instance of <xref:System.InvalidCas
141144
You are trying to convert a value or an object to its string representation by using a casting operator in C#. In the following example, both the attempt to cast a <xref:System.Char> value to a string and the attempt to cast an integer to a string throw an <xref:System.InvalidCastException> exception.
142145
143146
:::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/ToString1.cs" id="Snippet4":::
147+
:::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs" id="Snippet4":::
144148
145149
> [!NOTE]
146150
> Using the Visual Basic `CStr` operator to convert a value of a primitive type to a string succeeds. The operation does not throw an <xref:System.InvalidCastException> exception.
147151
148152
To successfully convert an instance of any type to its string representation, call its `ToString` method, as the following example does. The `ToString` method is always present, since the <xref:System.Object.ToString%2A> method is defined by the <xref:System.Object> class and therefore is either inherited or overridden by all managed types.
149153
150154
:::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/ToString2.cs" interactive="try-dotnet" id="Snippet5":::
155+
:::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs" id="Snippet5":::
151156
152157
<a name="Migration"></a>
153158
## Visual Basic 6.0 migration

0 commit comments

Comments
 (0)