{"record":{"id":"7b69198a6e7eb7fd","repo":"dotnet/wpf","slug":"sr-collection-badtype-pointcollection","errorCode":null,"errorMessage":"SR.Collection_BadType","messagePattern":"SR\\.Collection_BadType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs","lineNumber":455,"sourceCode":"        /// </summary>\n        internal Point Internal_GetItem(int i)\n        {\n            return _collection[i];\n        }\n\n\n\n        #endregion\n\n        #region Private Helpers\n\n        private Point Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is Point))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Point\"));\n            }\n\n            return (Point) value;\n        }\n\n        // IList.Add returns int and IList<T>.Add does not. This\n        // is called by both Adds and IList<T>'s just ignores the\n        // integer\n        private int AddHelper(Point value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs#L437-L473","documentation":"PointCollection.Cast(object) validates that any value added to the collection is a System.Windows.Point. If the value is null it throws ArgumentNullException; if it is not a Point it throws ArgumentException with Collection_BadType, reporting the collection type, the offending value's type, and the expected type 'Point'. This guards the non-generic IList surface (Add/Insert) where the compiler cannot enforce the element type.","triggerScenarios":"Calling ((IList)pointCollection).Add(someObject) or .Insert(index, someObject) with an object that is not a Point — e.g. adding a Point3D, a string like \"1,2\", a float, or a boxed value of the wrong type.","commonSituations":"Data binding or reflection-driven code that adds items to the non-generic IList interface; parsing code that produces strings instead of parsed Points; XAML-adjacent tooling passing variant objects; confusion between Point and Vector/Size structs.","solutions":["Pass an actual Point instance: points.Add(new Point(x, y));","If you have a string, parse it first with Point.Parse(s) (or TypeConverter) before adding.","Cast/convert the value to Point before calling the non-generic Add/Insert, or use PointCollection.Add(Point) so the compiler enforces the type."],"exampleFix":"// before\n((IList)points).Add(\"10,20\"); // ArgumentException Collection_BadType\n// after\n((IList)points).Add(Point.Parse(\"10,20\"));","handlingStrategy":"type-guard","validationCode":"if (value is not Point) throw new ArgumentException($\"Expected Point, got {value?.GetType().Name}\");","typeGuard":"static bool IsPoint(object v) => v is Point;","tryCatchPattern":"try { ((IList)points).Add(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadType\")) { /* convert or reject value */ }","preventionTips":["Prefer the generic Add(Point)/Insert(int, Point) overcasts so the compiler catches type errors.","Parse strings/coordinates with Point.Parse before insertion.","Never add Point3D/Vector/Size to a PointCollection — they are distinct structs."],"tags":["wpf","collection","argumentexception","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}