dotnet/wpf · error · ArgumentException
SR.ValueNotBetweenInt32MinMax
Error message
SR.ValueNotBetweenInt32MinMax
What it means
Window validates Left/Top values: NaN and PositiveInfinity are allowed sentinel values, and any finite value must be convertible to Int32. ArgumentException(SR.ValueNotBetweenInt32MinMax) is thrown when a finite double exceeds the Int32 range (l > Int32.MaxValue or l < Int32.MinValue), because window coordinates are ultimately stored as 32-bit pixel values.
Solutions
- Clamp the value into Int32 range before assigning: Math.Max(Int32.MinValue, Math.Min(Int32.MaxValue, value)).
- Validate persisted window-position config files for bogus Left/Top values and reset them to defaults when out of range.
- Fix the computation producing the oversized value (overflow, wrong units).
Example fix
// before window.Left = savedLeft; // may be 1e15 // after double left = Math.Max(Int32.MinValue, Math.Min(Int32.MaxValue, savedLeft)); if (!double.IsNaN(left)) window.Left = left;
Defensive patterns
Strategy: validation
Validate before calling
bool isValidLeft(double l) => double.IsNaN(l) || double.IsPositiveInfinity(l) || (l >= Int32.MinValue && l <= Int32.MaxValue);
Type guard
double? SafeCoord(double v) => (double.IsNaN(v) || double.IsPositiveInfinity(v) || (v >= Int32.MinValue && v <= Int32.MaxValue)) ? v : (double?)null;
Try / catch
try { window.Left = value; }
catch (ArgumentException) { window.Left = Double.NaN; /* center/default */ } Prevention
- Clamp persisted window coordinates to Int32 range when saving and loading layout.
- Check computed coordinates for overflow before assignment.
- Use Double.NaN (centered) instead of huge sentinel values.
When it happens
Trigger: Setting Window.Left or Window.Top (or calling the validating setter path) with a finite double outside [-2147483648, 2147483647], e.g. a computed coordinate that overflowed or was parsed from bad data.
Common situations: Restoring saved window positions from config with corrupted/huge values; accumulating offsets across loops; data-binding a coordinate to unvalidated numeric input; unit conversion mistakes (twips/DIPs) producing enormous values.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SR.InvalidValueForTopLeft
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- Collection_BadRank
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3becfdbe14133d1c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:5674
}
/// <summary>
/// Validate [Max/Min]Width/Height and Top/Left value.
/// </summary>
/// Length takes Double; Win32 handles Int.
/// We throw exception when the value goes below Int32Min and Int32Max.
/// WorkItem 26263: ValidateValueCallback needs to move to PropertyMetadata so Window can
/// add its own validation and validate before invalid value is set. Right now, we can only
/// validate this in PropertyInalidatinonCallback because of this. (We couldn't make it virtual on
/// FrameworkELement because ValidateValueCallback doesn't provide context. Work item 25275).
private static void ValidateLengthForHeightWidth(double l)
{
//basically, NaN and PositiveInfinity are ok, and then anything
//that can be converted to Int32
if (!Double.IsPositiveInfinity(l) && !double.IsNaN(l) &&
((l > Int32.MaxValue) || (l < Int32.MinValue)))
{
throw new ArgumentException(SR.Format(SR.ValueNotBetweenInt32MinMax, l));
}
}
private static void ValidateTopLeft(double length)
{
// Values not allowed: PositiveInfinity, NegativeInfinity
// and values that are beyond the range of Int32
if (Double.IsPositiveInfinity(length) ||
Double.IsNegativeInfinity(length))
{
throw new ArgumentException(SR.Format(SR.InvalidValueForTopLeft, length));
}
if ((length > Int32.MaxValue) ||
(length < Int32.MinValue))
{
throw new ArgumentException(SR.Format(SR.ValueNotBetweenInt32MinMax, length));
}View on GitHub (pinned to 81131a70a4)