{"record":{"id":"bf9ec5bf08de19a7","repo":"dotnet/wpf","slug":"sr-format-sr-invalidctorparameternonan-value-gridlength","errorCode":null,"errorMessage":"SR.Format(SR.InvalidCtorParameterNoNaN, \"value\")","messagePattern":"SR\\.Format\\(SR\\.InvalidCtorParameterNoNaN, \"value\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs","lineNumber":92,"sourceCode":"        /// </summary>\n        /// <param name=\"value\">Value to be stored by this GridLength \n        /// instance.</param>\n        /// <param name=\"type\">Type of the value to be stored by this GridLength \n        /// instance.</param>\n        /// <remarks> \n        /// If the <c>type</c> parameter is <c>GridUnitType.Auto</c>, \n        /// then passed in value is ignored and replaced with <c>0</c>.\n        /// </remarks>\n        /// <exception cref=\"ArgumentException\">\n        /// If <c>value</c> parameter is <c>double.NaN</c>\n        /// or <c>value</c> parameter is <c>double.NegativeInfinity</c>\n        /// or <c>value</c> parameter is <c>double.PositiveInfinity</c>.\n        /// </exception>\n        public GridLength(double value, GridUnitType type)\n        {\n            if (double.IsNaN(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, \"value\"));\n            }\n            if (double.IsInfinity(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoInfinity, \"value\"));\n            }\n            if (    type != GridUnitType.Auto\n                &&  type != GridUnitType.Pixel\n                &&  type != GridUnitType.Star   )\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, \"type\"));\n            }\n\n            _unitValue = (type == GridUnitType.Auto) ? 0.0 : value;\n            _unitType = type;\n        }\n\n        #endregion Constructors\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs#L74-L110","documentation":"The GridLength(double value, GridUnitType type) constructor validates that 'value' is a finite, non-NaN number. NaN is rejected with ArgumentException (InvalidCtorParameterNoNaN); infinity is rejected with InvalidCtorParameterNoInfinity; and only Auto (where the value is ignored) plus valid unit types are allowed.","triggerScenarios":"Calling new GridLength(double.NaN, ...) (NaN passed as the length value); the exception is thrown before the type checks, so any GridUnitType with a NaN value fails — except a NaN still throws even for GridUnitType.Auto.","commonSituations":"Passing an uncomputed double (result of 0.0/0.0, NaN from a binding or calculation) into the constructor; deserializing grid lengths from data where missing values become NaN; programmatic grid column/row definitions built from parsed or measured values.","solutions":["Validate the value before constructing: use double.IsNaN/IsInfinity checks and substitute a sensible default (e.g. GridLength.Auto or a fixed pixel size).","Use predefined lengths where possible: GridLength.Auto, new GridLength(1, GridUnitType.Star) instead of raw doubles.","Fix the upstream source producing NaN (e.g. empty parsed value or failed measurement) rather than defaulting at the call site."],"exampleFix":"// before\nvar len = new GridLength(parsedValue, GridUnitType.Pixel); // ArgumentException if parsedValue is NaN\n\n// after\nvar len = double.IsNaN(parsedValue) || double.IsInfinity(parsedValue)\n    ? GridLength.Auto\n    : new GridLength(parsedValue, GridUnitType.Pixel);","handlingStrategy":"validation","validationCode":"if (double.IsNaN(value) || double.IsInfinity(value))\n    throw new ArgumentException($\"GridLength value must be finite, got {value}.\");\nvar len = new GridLength(value, type);","typeGuard":"bool IsValidGridLength(double v) => !double.IsNaN(v) && !double.IsInfinity(v);","tryCatchPattern":"try\n{\n    var len = new GridLength(value, GridUnitType.Pixel);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"NaN\") || ex.Message.Contains(\"value\"))\n{\n    Log.Warn($\"Invalid GridLength value {value}; falling back to Auto.\", ex);\n}","preventionTips":["Check double.IsNaN/IsInfinity before constructing GridLength from computed or parsed values.","Prefer GridLength.Auto and Star-unit constructors over raw doubles where semantics allow.","Sanitize deserialized/bound numeric inputs so missing data never yields NaN."],"tags":["wpf","argument","nan","gridlength","validation"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}