Skip to content

Commit 145fb68

Browse files
authored
UInt16 F# snippets (#8050)
1 parent e62cf19 commit 145fb68

File tree

21 files changed

+584
-6
lines changed

21 files changed

+584
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//<snippet3>
2+
open System
3+
4+
type Temperature() =
5+
// The value holder
6+
let mutable m_value = 0us
7+
8+
interface IComparable with
9+
/// IComparable.CompareTo implementation.
10+
member _.CompareTo(obj) =
11+
match obj with
12+
| :? Temperature as temp ->
13+
m_value.CompareTo temp.Value
14+
| _ ->
15+
invalidArg "obj" "object is not a Temperature"
16+
17+
member _.Value
18+
with get () =
19+
m_value
20+
and set (v) =
21+
m_value <- v
22+
//</snippet3>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module equalsoverl
2+
3+
// <Snippet1>
4+
let value = 112us
5+
6+
let testObjectForEquality (obj: obj) =
7+
printfn $"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals obj}\n"
8+
9+
let byte1= 112uy
10+
printfn $"value = byte1: {value.Equals byte1,16}"
11+
testObjectForEquality byte1
12+
13+
let short1 = 112s
14+
printfn $"value = short1: {value.Equals short1,17}"
15+
testObjectForEquality short1
16+
17+
let int1 = 112
18+
printfn $"value = int1: {value.Equals int1,19}"
19+
testObjectForEquality int1
20+
21+
let sbyte1 = 112y
22+
printfn $"value = sbyte1: {value.Equals sbyte1,17}"
23+
testObjectForEquality sbyte1
24+
25+
let dec1 = 112m
26+
printfn $"value = dec1: {value.Equals dec1,21}"
27+
testObjectForEquality dec1
28+
29+
let dbl1 = 112.
30+
printfn $"value = dbl1: {value.Equals dbl1,20}"
31+
testObjectForEquality dbl1
32+
33+
// The example displays the following output:
34+
// value = byte1: True
35+
// 112 (UInt16) = 112 (Byte): False
36+
//
37+
// value = short1: False
38+
// 112 (UInt16) = 112 (Int16): False
39+
//
40+
// value = int1: False
41+
// 112 (UInt16) = 112 (Int32): False
42+
//
43+
// value = sbyte1: False
44+
// 112 (UInt16) = 112 (SByte): False
45+
//
46+
// value = dec1: False
47+
// 112 (UInt16) = 112 (Decimal): False
48+
//
49+
// value = dbl1: False
50+
// 112 (UInt16) = 112 (Double): False
51+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="uint16_equals.fs" />
9+
<Compile Include="equalsoverl.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module uint16_equals
2+
3+
// <Snippet1>
4+
let myVariable1 = 10us
5+
let myVariable2 = 10us
6+
7+
//Display the declaring type.
8+
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is :{myVariable1}"
9+
printfn $"Type of 'myVariable2' is '{myVariable2.GetType()}' and value is :{myVariable2}"
10+
11+
// Compare 'myVariable1' instance with 'myVariable2' Object.
12+
if myVariable1.Equals myVariable2 then
13+
printfn $"\nStructures 'myVariable1' and 'myVariable2' are equal"
14+
else
15+
printfn $"\nStructures 'myVariable1' and 'myVariable2' are not equal"
16+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// <Snippet1>
2+
open System
3+
4+
let integerValue = 1216
5+
6+
if integerValue >= int UInt16.MinValue && integerValue <= int UInt16.MaxValue then
7+
let uIntegerValue = uint16 integerValue
8+
printfn $"{uIntegerValue}"
9+
else
10+
printfn $"Unable to convert {integerValue} to a UInt16t."
11+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="MaxValue.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="parseex5.fs" />
9+
<Compile Include="parseex2.fs" />
10+
<Compile Include="parseex3.fs" />
11+
<Compile Include="parseex4.fs" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module parseex2
2+
3+
// <Snippet6>
4+
open System
5+
open System.Globalization
6+
7+
let values = [| " 214 "; "1,064"; "(0)"; "1241+"; " + 214 "; " +214 "; "2153.0"; "1e03"; "1300.0e-2" |]
8+
let whitespace = NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
9+
let styles =
10+
[| NumberStyles.None; whitespace
11+
NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace
12+
NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol
13+
NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]
14+
15+
// Attempt to convert each number using each style combination.
16+
for value in values do
17+
printfn $"Attempting to convert '{value}':"
18+
for style in styles do
19+
try
20+
let number = UInt16.Parse(value, style)
21+
printfn $" {style}: {number}"
22+
with :? FormatException ->
23+
printfn $" {style}: Bad Format"
24+
printfn ""
25+
// The example display the following output:
26+
// Attempting to convert ' 214 ':
27+
// None: Bad Format
28+
// AllowLeadingWhite, AllowTrailingWhite: 214
29+
// Integer, AllowTrailingSign: 214
30+
// AllowThousands, AllowCurrencySymbol: Bad Format
31+
// AllowDecimalPoint, AllowExponent: Bad Format
32+
//
33+
// Attempting to convert '1,064':
34+
// None: Bad Format
35+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
36+
// Integer, AllowTrailingSign: Bad Format
37+
// AllowThousands, AllowCurrencySymbol: 1064
38+
// AllowDecimalPoint, AllowExponent: Bad Format
39+
//
40+
// Attempting to convert '(0)':
41+
// None: Bad Format
42+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
43+
// Integer, AllowTrailingSign: Bad Format
44+
// AllowThousands, AllowCurrencySymbol: Bad Format
45+
// AllowDecimalPoint, AllowExponent: Bad Format
46+
//
47+
// Attempting to convert '1241+':
48+
// None: Bad Format
49+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
50+
// Integer, AllowTrailingSign: 1241
51+
// AllowThousands, AllowCurrencySymbol: Bad Format
52+
// AllowDecimalPoint, AllowExponent: Bad Format
53+
//
54+
// Attempting to convert ' + 214 ':
55+
// None: Bad Format
56+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
57+
// Integer, AllowTrailingSign: Bad Format
58+
// AllowThousands, AllowCurrencySymbol: Bad Format
59+
// AllowDecimalPoint, AllowExponent: Bad Format
60+
//
61+
// Attempting to convert ' +214 ':
62+
// None: Bad Format
63+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
64+
// Integer, AllowTrailingSign: 214
65+
// AllowThousands, AllowCurrencySymbol: Bad Format
66+
// AllowDecimalPoint, AllowExponent: Bad Format
67+
//
68+
// Attempting to convert '2153.0':
69+
// None: Bad Format
70+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
71+
// Integer, AllowTrailingSign: Bad Format
72+
// AllowThousands, AllowCurrencySymbol: Bad Format
73+
// AllowDecimalPoint, AllowExponent: 2153
74+
//
75+
// Attempting to convert '1e03':
76+
// None: Bad Format
77+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
78+
// Integer, AllowTrailingSign: Bad Format
79+
// AllowThousands, AllowCurrencySymbol: Bad Format
80+
// AllowDecimalPoint, AllowExponent: 1000
81+
//
82+
// Attempting to convert '1300.0e-2':
83+
// None: Bad Format
84+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
85+
// Integer, AllowTrailingSign: Bad Format
86+
// AllowThousands, AllowCurrencySymbol: Bad Format
87+
// AllowDecimalPoint, AllowExponent: 13
88+
// </Snippet6>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module parseex3
2+
3+
// <Snippet3>
4+
open System
5+
open System.Globalization
6+
7+
// Define a custom culture that uses "++" as a positive sign.
8+
let ci = CultureInfo ""
9+
ci.NumberFormat.PositiveSign <- "++"
10+
// Create an array of cultures.
11+
let cultures = [| ci; CultureInfo.InvariantCulture |]
12+
// Create an array of strings to parse.
13+
let values =
14+
[| "++1403"; "-0"; "+0"; "+16034"
15+
string Int16.MinValue; "14.0"; "18012" |]
16+
// Parse the strings using each culture.
17+
for culture in cultures do
18+
printfn $"Parsing with the '{culture.Name}' culture."
19+
for value in values do
20+
try
21+
let number = UInt16.Parse(value, culture)
22+
printfn $" Converted '{value}' to {number}."
23+
with
24+
| :? FormatException ->
25+
printfn $" The format of '{value}' is invalid."
26+
| :? OverflowException ->
27+
printfn $" '{value}' is outside the range of a UInt16 value."
28+
// The example displays the following output:
29+
// Parsing with the culture.
30+
// Converted '++1403' to 1403.
31+
// Converted '-0' to 0.
32+
// The format of '+0' is invalid.
33+
// The format of '+16034' is invalid.
34+
// '-32768' is outside the range of a UInt16 value.
35+
// The format of '14.0' is invalid.
36+
// Converted '18012' to 18012.
37+
// Parsing with the '' culture.
38+
// The format of '++1403' is invalid.
39+
// Converted '-0' to 0.
40+
// Converted '+0' to 0.
41+
// Converted '+16034' to 16034.
42+
// '-32768' is outside the range of a UInt16 value.
43+
// The format of '14.0' is invalid.
44+
// Converted '18012' to 18012.
45+
// </Snippet3>

0 commit comments

Comments
 (0)