Skip to content

Commit 89bc21b

Browse files
authored
UInt32 F# snippets (#8046)
1 parent b9fbf25 commit 89bc21b

File tree

20 files changed

+629
-0
lines changed

20 files changed

+629
-0
lines changed
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="tryparse11.fs" />
9+
<Compile Include="tryparse21.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module tryparse11
2+
3+
open System
4+
5+
// <Snippet1>
6+
let numericStrings =
7+
[ "1293.8"; "+1671.7"; "28347."
8+
" 33113684 "; "(0)"; "-0"; "-1"
9+
"+1293617"; "18-"; "119870"; "31,024"
10+
" 3127094 "; "00700000" ]
11+
12+
for numericString in numericStrings do
13+
match UInt32.TryParse numericString with
14+
| true, number ->
15+
printfn $"Converted '{numericString}' to {number}."
16+
| _ ->
17+
printfn $"Cannot convert '{numericString}' to a UInt32."
18+
// The example displays the following output:
19+
// Cannot convert '1293.8' to a UInt32.
20+
// Cannot convert '+1671.7' to a UInt32.
21+
// Cannot convert '28347.' to a UInt32.
22+
// Converted ' 33113684 ' to 33113684.
23+
// Cannot convert '(0)' to a UInt32.
24+
// Converted '-0' to 0.
25+
// Cannot convert '-1' to a UInt32.
26+
// Converted '+1293617' to 1293617.
27+
// Cannot convert '18-' to a UInt32.
28+
// Converted '119870' to 119870.
29+
// Cannot convert '31,024' to a UInt32.
30+
// Converted ' 3127094 ' to 3127094.
31+
// Converted '0070000' to 70000.
32+
// </Snippet1>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module tryparse21
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
8+
match UInt32.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
9+
| true, number ->
10+
printfn $"Converted '{stringToConvert}' to {number}."
11+
printfn $"Attempted conversion of '{stringToConvert}' failed."
12+
| _ -> ()
13+
14+
do
15+
let numericString = "2106034"
16+
let styles = NumberStyles.Integer
17+
callTryParse numericString styles
18+
19+
let numericString = "-10603"
20+
let styles = NumberStyles.None
21+
callTryParse numericString styles
22+
23+
let numericString = "29103674.00"
24+
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
25+
callTryParse numericString styles
26+
27+
let numericString = "10345.72"
28+
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
29+
callTryParse numericString styles
30+
31+
let numericString = "41792210E-01"
32+
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
33+
callTryParse numericString styles
34+
35+
let numericString = "9112E-01"
36+
callTryParse numericString styles
37+
38+
let numericString = "312E01"
39+
callTryParse numericString styles
40+
41+
let numericString = "FFC86DA1"
42+
callTryParse numericString NumberStyles.HexNumber
43+
44+
let numericString = "0x8F8C"
45+
callTryParse numericString NumberStyles.HexNumber
46+
47+
// The example displays the following output:
48+
// Converted '2106034' to 2106034.
49+
// Attempted conversion of '-10603' failed.
50+
// Converted '29103674.00' to 29103674.
51+
// Attempted conversion of '10345.72' failed.
52+
// Converted '41792210E-01' to 4179221.
53+
// Attempted conversion of '9112E-01' failed.
54+
// Converted '312E01' to 3120.
55+
// Converted 'FFC86DA1' to 4291325345.
56+
// Attempted conversion of '0x8F8C' failed.
57+
// </Snippet2>
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Snippets2
2+
3+
open System
4+
5+
//<snippet2>
6+
type Temperature() =
7+
// The value holder
8+
let mutable m_value = 0u
9+
10+
static member MinValue =
11+
UInt32.MinValue
12+
13+
static member MaxValue =
14+
UInt32.MaxValue
15+
16+
member _.Value
17+
with get () =
18+
m_value
19+
and set (v) =
20+
m_value <- v
21+
//</snippet2>
22+
23+
namespace Snippets3
24+
25+
open System
26+
//<snippet3>
27+
type Temperature() =
28+
// The value holder
29+
let mutable m_value = 0u
30+
31+
member _.Value
32+
with get () =
33+
m_value
34+
and set (v) =
35+
m_value <- v
36+
37+
interface IComparable with
38+
/// IComparable.CompareTo implementation.
39+
member _.CompareTo(obj) =
40+
match obj with
41+
| :? Temperature as temp ->
42+
m_value.CompareTo temp.Value
43+
| _ ->
44+
invalidArg "obj" "object is not a Temperature"
45+
46+
//</snippet3>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module equalsoverl
2+
3+
// <Snippet1>
4+
let value = 112u
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 long1 = 112L
18+
printfn $"value = long1: {value.Equals long1,18}"
19+
testObjectForEquality long1
20+
21+
let sbyte1 = 112y
22+
printfn $"value = sbyte1: {value.Equals sbyte1,17}"
23+
testObjectForEquality sbyte1
24+
25+
let ushort1 = 112us
26+
printfn $"value = ushort1: {value.Equals ushort1,16}"
27+
testObjectForEquality ushort1
28+
29+
let ulong1 = 112uL
30+
printfn $"value = ulong1: {value.Equals ulong1,18}"
31+
testObjectForEquality ulong1
32+
33+
let dec1 = 112m
34+
printfn $"value = dec1: {value.Equals dec1,21}"
35+
testObjectForEquality dec1
36+
37+
let dbl1 = 112.
38+
printfn $"value = dbl1: {value.Equals dbl1,20}"
39+
testObjectForEquality dbl1
40+
41+
// The example displays the following output:
42+
// value = byte1: True
43+
// 112 (UInt32) = 112 (Byte): False
44+
//
45+
// value = short1: False
46+
// 112 (UInt32) = 112 (Int16): False
47+
//
48+
// value = long1: False
49+
// 112 (UInt32) = 112 (Int64): False
50+
//
51+
// value = sbyte1: False
52+
// 112 (UInt32) = 112 (SByte): False
53+
//
54+
// value = ushort1: True
55+
// 112 (UInt32) = 112 (UInt16): False
56+
//
57+
// value = ulong1: False
58+
// 112 (UInt32) = 112 (UInt64): False
59+
//
60+
// value = dec1: False
61+
// 112 (UInt32) = 112 (Decimal): False
62+
//
63+
// value = dbl1: False
64+
// 112 (UInt32) = 112 (Double): False
65+
// </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="uint32_equals.fs" />
9+
<Compile Include="equalsoverl.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module uint32_equals
2+
// <Snippet1>
3+
let myVariable1 = 20u
4+
let myVariable2 = 20u
5+
6+
// Display the declaring type.
7+
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is :{myVariable1}"
8+
printfn $"Type of 'myVariable2' is '{myVariable2.GetType()}' and value is :{myVariable2}"
9+
10+
// Compare 'myVariable1' instance with 'myVariable2' Object.
11+
if myVariable1.Equals myVariable2 then
12+
printfn $"\nStructures 'myVariable1' and 'myVariable2' are equal"
13+
else
14+
printfn $"\nStructures 'myVariable1' and 'myVariable2' are not equal"
15+
// </Snippet1>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
open System
2+
3+
// <Snippet1>
4+
let longValue = Int64.MaxValue / 2L
5+
6+
if longValue <= int64 UInt32.MaxValue && longValue >= int64 UInt32.MinValue then
7+
let integerValue = uint longValue
8+
printfn $"Converted long integer value to {integerValue:n0}."
9+
else
10+
let rangeLimit, relationship =
11+
if longValue > int64 UInt32.MaxValue then
12+
UInt32.MaxValue, "greater"
13+
else
14+
UInt32.MinValue, "less"
15+
16+
printfn $"Conversion failure: {longValue:n0} is {relationship} than {rangeLimit:n0}"
17+
// </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="MaxValue1.fs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)