.NET Framework Class Library
UInt16..::.MaxValue Field

Represents the largest possible value of UInt16. This field is constant.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Const MaxValue As UShort
Visual Basic (Usage)
Dim value As UShort

value = UShort.MaxValue
C#
public const ushort MaxValue
Visual C++
public:
literal unsigned short MaxValue
JScript
public const var MaxValue : ushort
Remarks

The value of this constant is 65535; that is, hexadecimal 0xFFFF.

Examples

The following example uses the MaxValue and MinValue properties to ensure that an Int32 value is in the range of the UInt16 type before converting it to a UInt16 value. This prevents the conversion operation from throwing an OverflowException if the integer value is not in the range of the UInt16 type.

Visual Basic
Dim integerValue As Integer = 1216
Dim uIntegerValue As UShort

If integerValue >= UShort.MinValue And integerValue <= UShort.MaxValue Then
   uIntegerValue = CUShort(integerValue) 
   Console.WriteLine(uIntegerValue)
Else
   Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue)   
End If   
C#
int integerValue = 1216; 
ushort uIntegerValue;

if (integerValue >= ushort.MinValue & integerValue <= ushort.MaxValue)
{
   uIntegerValue = (ushort) integerValue;
   Console.WriteLine(uIntegerValue);
} 
else
{
   Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue);
}     
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Community Content

Thomas Lee
Uint Maxvalue/MinValue sampe using PowerShell
# get-uint16maxvalue.ps1
# Maxvalue sample using PowerShell
# Thomas Lee - tfl@psp.co.uk
 
# Create test a value in range of uint16
[int] $IntegerValue = 1216
[system.int16] $uIntegerValue = 0
 
# Print it out
"Test 1"
"Integer value = $Integervalue"
 
# Now test and print results
if ($integerValue -ge [System.uint16]::MinValue -and $integerValue -le [System.uint16]::MaxValue) {
$uIntegerValue = $integerValue
"The short value is: $uIntegerValue"
}
else
{
"Unable to convert {0} to a UInt16" -f $integerValue
}
""
 
# now test one out of range
"Test 2"
if (99999999 -ge [System.uint16]::MinValue -and 99999999 -le [System.uint16]::MaxValue) {
$uIntegerValue = 99999999
"The short value is: $uIntegerValue"
}
else
{
"Unable to convert {0} to a UInt16" -f 99999999
}

#

This script produces the following output:

PSH [C:\foo]: .\get-uint16fields.ps1
Test 1
Integer value = 1216
The short value is: 1216
Test 2
Unable to convert 99999999 to a UInt16
 

Page view tracker