Byte::TryParse Method (String^, Byte%)
Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- s
-
Type:
System::String^
A string that contains a number to convert. The string is interpreted using the Integer style.
- result
-
Type:
System::Byte%
When this method returns, contains the Byte value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
The conversion fails and the method returns false if the s parameter is not in the correct format, if it is null or String::Empty, or if it represents a number less than MinValue or greater than MaxValue.
The Byte::TryParse(String^, Byte%) method is similar to the Byte::Parse(String^) method, except that TryParse(String^, Byte%) does not throw an exception if the conversion fails.
The s parameter should be the string representation of a number in the following form:
[ws][sign]digits[ws]
Elements in square brackets ([ and ]) are optional. The following table describes each element.
Element | Description |
|---|---|
ws | Optional white space. |
sign | An optional positive sign, as specified by the NumberFormatInfo::PositiveSign property of the current culture. |
digits | A sequence of decimal digits that range from 0 to 9. |
The s parameter is interpreted using the Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Byte::Parse(String^, NumberStyles, IFormatProvider^) method.
The s parameter is parsed using the formatting information in a NumberFormatInfo object for the current culture. For more information, see NumberFormatInfo::CurrentInfo.
This overload of the Byte::TryParse(String^, Byte%) method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Byte::TryParse(String^, NumberStyles, IFormatProvider^, Byte%) overload.
The following example calls the TryParse(String^, Byte%) method with a number of different string values.
using namespace System; void main() { array<String^>^ byteStrings = gcnew array<String^> { nullptr, String::Empty, "1024", "100.1", "100", "+100", "-100", "000000000000000100", "00,100", " 20 ", "FF", "0x1F" }; Byte byteValue; for each (String^ byteString in byteStrings) { bool result = Byte::TryParse(byteString, byteValue); if (result) Console::WriteLine("Converted '{0}' to {1}", byteString, byteValue); else Console::WriteLine("Attempted conversion of '{0}' failed.", byteString); } } // The example displays the following output: // Attempted conversion of '' failed. // Attempted conversion of '' failed.` // Attempted conversion of '1024' failed. // Attempted conversion of '100.1' failed. // Converted '100' to 100 // Converted '+100' to 100 // Attempted conversion of '-100' failed. // Converted '000000000000000100' to 100 // Attempted conversion of '00,100' failed. // Converted ' 20 ' to 20 // Attempted conversion of 'FF' failed. // Attempted conversion of '0x1F' failed.}
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1