{"record":{"id":"7b7d35501a7315fa","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-nameof-percentagewithinlasso","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(nameof(percentageWithinLasso))","messagePattern":"ArgumentOutOfRangeException\\(nameof\\(percentageWithinLasso\\)\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/Stroke2.cs","lineNumber":259,"sourceCode":"            {\n                //detach from event handlers, or else we leak.\n                strokeInfo?.Detach();\n            }\n        }\n\n        /// <summary>\n        /// Check whether a certain percentage of the stroke is within the lasso\n        /// </summary>\n        /// <param name=\"lassoPoints\"></param>\n        /// <param name=\"percentageWithinLasso\"></param>\n        /// <returns></returns>\n        public bool HitTest(IEnumerable<Point> lassoPoints, int percentageWithinLasso)\n        {\n            ArgumentNullException.ThrowIfNull(lassoPoints);\n\n            if ((percentageWithinLasso < 0) || (percentageWithinLasso > 100))\n            {\n                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinLasso));\n            }\n\n            if (percentageWithinLasso == 0)\n            {\n                return true;\n            }\n\n\n            StrokeInfo strokeInfo = null;\n            try\n            {\n                strokeInfo = new StrokeInfo(this);\n\n                StylusPointCollection stylusPoints = strokeInfo.StylusPoints;\n                double target = strokeInfo.TotalWeight * percentageWithinLasso / 100.0f - PercentageTolerance;\n\n                Lasso lasso = new SingleLoopLasso();\n                lasso.AddPoints(lassoPoints);","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/Stroke2.cs#L241-L277","documentation":"Stroke.HitTest(IEnumerable<Point> lassoPoints, int percentageWithinLasso) checks what percentage of the stroke falls inside a lasso polygon. percentageWithinLasso must be 0-100 or ArgumentOutOfRangeException(nameof(percentageWithinLasso)) is thrown; 0 returns true immediately. Null lassoPoints is also rejected via ArgumentNullException.","triggerScenarios":"Passing a negative or >100 percentage from an unvalidated UI setting; passing null or an empty lasso point collection alongside the bad percentage.","commonSituations":"Lasso-selection sensitivity sliders bound without range checks; computed percentages from coverage math that exceeded 100 due to rounding or bad totals.","solutions":["Clamp percentageWithinLasso to 0-100 before calling","Validate the sensitivity setting when it is loaded from config/UI","Also ensure lassoPoints is non-null and non-empty before invoking"],"exampleFix":"// before\nbool hit = stroke.HitTest(lasso, pct);\n// after\nint pct = Math.Clamp(pct, 0, 100);\nbool hit = stroke.HitTest(lasso, pct);","handlingStrategy":"validation","validationCode":"bool valid = percentageWithinLasso is >= 0 and <= 100 && lassoPoints is not null && lassoPoints.Any();","typeGuard":"static bool IsValidLassoHit(IEnumerable<Point>? pts, int pct) => pts is not null && pts.Any() && pct is >= 0 and <= 100;","tryCatchPattern":"try { hit = stroke.HitTest(lasso, pct); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"percentageWithinLasso\") { hit = stroke.HitTest(lasso, Math.Clamp(pct, 0, 100)); }","preventionTips":["Clamp lasso percentages to 0-100 before calls","Bind sensitivity UI with min=0 max=100","Validate both points and percentage together"],"tags":["wpf","ink","argument-out-of-range","lasso"],"backgroundTag":"value-out-of-range","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"}