forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.vb
146 lines (129 loc) · 5.08 KB
/
parse.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
' Visual Basic .NET Document
Option Strict On
Imports System.Globalization
Module modMain
Public Sub Main()
CallParse1()
Console.WriteLine()
CallParse2()
Console.WriteLine()
CallParse3()
Console.WriteLine()
CallParse4()
End Sub
Private Sub CallParse1()
' <Snippet1>
Dim stringToConvert As String = " 162"
Dim byteValue As Byte
Try
byteValue = Byte.Parse(stringToConvert)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
' The example displays the following output to the console:
' Converted ' 162' to 162.
' </Snippet1>
End Sub
Private Sub CallParse2()
' <Snippet2>
Dim stringToConvert As String
Dim byteValue As Byte
stringToConvert = " 214 "
Try
byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
stringToConvert = " + 214 "
Try
byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
stringToConvert = " +214 "
Try
byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
' The example displays the following output to the console:
' Converted ' 214 ' to 214.
' Unable to parse ' + 214 '.
' Converted ' +214 ' to 214.
' </Snippet2>
End Sub
Private Sub CallParse3()
' <Snippet3>
Dim value As String
Dim style As NumberStyles
Dim number As Byte
' Parse value with no styles allowed.
style = NumberStyles.None
value = " 241 "
Try
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
value = " 163+"
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' Parse value with leading sign.
value = " +253 "
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' This example displays the following output to the console:
' Unable to parse ' 241 '.
' Converted ' 163+' to 163.
' Converted ' +253 ' to 253.
' </Snippet3>
End Sub
Private Sub CallParse4()
' <Snippet4>
Dim style As NumberStyles
Dim culture As CultureInfo
Dim value As String
Dim number As Byte
' Parse number with decimals.
' NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float
culture = CultureInfo.CreateSpecificCulture("fr-FR")
value = "12,000"
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
culture = CultureInfo.CreateSpecificCulture("en-GB")
Try
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
value = "12.000"
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' The example displays the following output to the console:
' Converted '12,000' to 12.
' Unable to parse '12,000'.
' Converted '12.000' to 12.
' </Snippet4>
End Sub
End Module