{"record":{"id":"aaf170330ffea846","repo":"dotnet/wpf","slug":"unit","errorCode":null,"errorMessage":"unit","messagePattern":"unit","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointPropertyInfo.cs","lineNumber":50,"sourceCode":"            _resolution = info.Resolution;\n            _unit = info.Unit;\n}\n\n        /// <summary>\n        /// StylusPointProperty\n        /// </summary>\n        /// <param name=\"stylusPointProperty\"></param>\n        /// <param name=\"minimum\">minimum</param>\n        /// <param name=\"maximum\">maximum</param>\n        /// <param name=\"unit\">unit</param>\n        /// <param name=\"resolution\">resolution</param>\n        public StylusPointPropertyInfo(StylusPointProperty stylusPointProperty, int minimum, int maximum, StylusPointPropertyUnit unit, float resolution)\n            : base(stylusPointProperty) //base checks for null\n        {\n            // validate unit\n            if (!StylusPointPropertyUnitHelper.IsDefined(unit))\n            {\n                throw new InvalidEnumArgumentException(\"unit\", (int)unit, typeof(StylusPointPropertyUnit));\n            }\n\n            // validate min/max\n            if (maximum < minimum)\n            {\n                throw new ArgumentException(SR.Stylus_InvalidMax, nameof(maximum));\n            }\n\n            // validate resolution\n            if (resolution < 0.0f)\n            {\n                throw new ArgumentException(SR.InvalidStylusPointPropertyInfoResolution, nameof(resolution));\n            }\n\n            _min = minimum;\n            _max = maximum;\n            _resolution = resolution;\n            _unit = unit;","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointPropertyInfo.cs#L32-L68","documentation":"The StylusPointPropertyInfo constructor validates the unit parameter via StylusPointPropertyUnitHelper.IsDefined and throws InvalidEnumArgumentException when the unit value is not a defined StylusPointPropertyUnit member. The base constructor already guaranteed stylusPointProperty is non-null, so this error is specifically about an out-of-range/undefined enum value (message string 'unit' is the parameter name).","triggerScenarios":"Calling new StylusPointPropertyInfo(prop, min, max, (StylusPointPropertyUnit)someInt, resolution) where someInt is not a defined enum value, e.g. a value read from untrusted device data or a cast of an unrelated enum.","commonSituations":"Building property infos from raw device/unit codes where the device reports a unit WPF does not define; casting an int from a config file or P/Invoke structure directly into StylusPointPropertyUnit.","solutions":["Use only defined members: StylusPointPropertyUnit.None, Inches, Centimeters, Degrees, Radians.","Validate/sanitize external unit codes with Enum.IsDefined(typeof(StylusPointPropertyUnit), value) before casting, defaulting to None otherwise.","Map unsupported device units to the closest defined WPF unit (typically None or Inches) and record the raw value separately."],"exampleFix":"// before\nvar unit = (StylusPointPropertyUnit)rawDeviceUnit; // may be undefined\nvar info = new StylusPointPropertyInfo(prop, min, max, unit, res);\n// after\nvar unit = Enum.IsDefined(typeof(StylusPointPropertyUnit), rawDeviceUnit)\n    ? (StylusPointPropertyUnit)rawDeviceUnit\n    : StylusPointPropertyUnit.None;\nvar info = new StylusPointPropertyInfo(prop, min, max, unit, res);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(StylusPointPropertyUnit), unitValue)) unitValue = (int)StylusPointPropertyUnit.None;","typeGuard":"static bool IsDefinedUnit(StylusPointPropertyUnit u) => u is StylusPointPropertyUnit.None or StylusPointPropertyUnit.Inches or StylusPointPropertyUnit.Centimeters or StylusPointPropertyUnit.Degrees or StylusPointPropertyUnit.Radians;","tryCatchPattern":"try { var info = new StylusPointPropertyInfo(prop, min, max, unit, res); }\ncatch (InvalidEnumArgumentException) { info = new StylusPointPropertyInfo(prop, min, max, StylusPointPropertyUnit.None, res); }","preventionTips":["Map external device unit codes to defined enum members before casting","Default unknown units to StylusPointPropertyUnit.None","Never cast raw ints from P/Invoke structures without Enum.IsDefined"],"tags":["wpf","stylus","invalid-enum","argument"],"backgroundTag":"invalid-enum-value","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"}