From 6392454e434e91abde16de2657ac74a43888c538 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Sat, 26 Feb 2022 13:07:43 -0800 Subject: [PATCH] InvalidCastException F# snippets --- .../Overview/Interface1.fs | 17 +++++++++ .../Overview/ToString1.fs | 7 ++++ .../Overview/ToString2.fs | 9 +++++ .../Overview/basetoderived1.fs | 35 +++++++++++++++++++ .../InvalidCastException/Overview/fs.fsproj | 14 ++++++++ .../Overview/iconvertible1.fs | 23 ++++++++++++ xml/System/InvalidCastException.xml | 5 +++ 7 files changed, 110 insertions(+) create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/basetoderived1.fs create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs diff --git a/snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs b/snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs new file mode 100644 index 00000000000..68ac52ee72d --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs @@ -0,0 +1,17 @@ +module Interface1 + +// +open System +open System.Globalization + +let culture = CultureInfo.InvariantCulture +let provider: IFormatProvider = culture + +let dt = provider :?> DateTimeFormatInfo + +// The example displays the following output: +// Unhandled Exception: System.InvalidCastException: +// Unable to cast object of type //System.Globalization.CultureInfo// to +// type //System.Globalization.DateTimeFormatInfo//. +// at Example.main() +// \ No newline at end of file diff --git a/snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs b/snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs new file mode 100644 index 00000000000..a5ac598ee01 --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs @@ -0,0 +1,7 @@ +module ToString1 + +// +let value: obj = 12 +// Cast throws an InvalidCastException exception. +let s = value :?> string +// \ No newline at end of file diff --git a/snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs b/snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs new file mode 100644 index 00000000000..07f9dddbe0e --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs @@ -0,0 +1,9 @@ +module ToString2 + +// +let value: obj = 12 +let s = value.ToString() +printfn $"{s}" +// The example displays the following output: +// 12 +// \ No newline at end of file diff --git a/snippets/fsharp/System/InvalidCastException/Overview/basetoderived1.fs b/snippets/fsharp/System/InvalidCastException/Overview/basetoderived1.fs new file mode 100644 index 00000000000..c31c4389884 --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/basetoderived1.fs @@ -0,0 +1,35 @@ +module basetoderived1 + +// +open System + +type Person() = + member val Name = String.Empty with get, set + +type PersonWithId() = + inherit Person() + member val Id = String.Empty with get, set + + +let p = Person() +p.Name <- "John" +try + let pid = p :?> PersonWithId + printfn "Conversion succeeded." +with :? InvalidCastException -> + printfn "Conversion failed." + +let pid1 = PersonWithId() +pid1.Name <- "John" +pid1.Id <- "246" +let p1: Person = pid1 +try + let pid1a = p1 :?> PersonWithId + printfn "Conversion succeeded." +with :? InvalidCastException -> + printfn "Conversion failed." + +// The example displays the following output: +// Conversion failed. +// Conversion succeeded. +// \ No newline at end of file diff --git a/snippets/fsharp/System/InvalidCastException/Overview/fs.fsproj b/snippets/fsharp/System/InvalidCastException/Overview/fs.fsproj new file mode 100644 index 00000000000..84f65a7975c --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/fs.fsproj @@ -0,0 +1,14 @@ + + + Exe + net6.0 + + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs b/snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs new file mode 100644 index 00000000000..eccfe171f30 --- /dev/null +++ b/snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs @@ -0,0 +1,23 @@ +module iconvertible1 + +// +open System + +let flag = true +try + let conv: IConvertible = flag + let ch = conv.ToChar null + printfn "Conversion succeeded." +with :? InvalidCastException -> + printfn "Cannot convert a Boolean to a Char." + +try + let ch = Convert.ToChar flag + printfn "Conversion succeeded." +with :? InvalidCastException -> + printfn "Cannot convert a Boolean to a Char." + +// The example displays the following output: +// Cannot convert a Boolean to a Char. +// Cannot convert a Boolean to a Char. +// \ No newline at end of file diff --git a/xml/System/InvalidCastException.xml b/xml/System/InvalidCastException.xml index ac255c66a77..0dd7b3b5984 100644 --- a/xml/System/InvalidCastException.xml +++ b/xml/System/InvalidCastException.xml @@ -100,6 +100,7 @@ For a list of initial property values for an instance of implementation that does not support a particular conversion. For example, trying to convert a value to a or a value to an throws an exception. The following example calls both the and methods to convert a value to a . In both cases, the method call throws an exception. :::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/iconvertible1.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/iconvertible1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidcastexception/vb/iconvertible1.vb" id="Snippet2"::: 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 exception when it attempts to convert an object to a object. The conversion fails because although the class implements the interface, the object is not related to the class from which the interface object was derived. :::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/Interface1.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/Interface1.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidcastexception/vb/Interface1.vb" id="Snippet3"::: 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 . The conversion would also succeed if the interface object is converted to an instance of a base type of the original type. @@ -141,6 +144,7 @@ For a list of initial property values for an instance of value to a string and the attempt to cast an integer to a string throw an exception. :::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/ToString1.cs" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/ToString1.fs" id="Snippet4"::: > [!NOTE] > Using the Visual Basic `CStr` operator to convert a value of a primitive type to a string succeeds. The operation does not throw an exception. @@ -148,6 +152,7 @@ For a list of initial property values for an instance of method is defined by the class and therefore is either inherited or overridden by all managed types. :::code language="csharp" source="~/snippets/csharp/System/InvalidCastException/Overview/ToString2.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/InvalidCastException/Overview/ToString2.fs" id="Snippet5"::: ## Visual Basic 6.0 migration