{"record":{"id":"7b367dd00d271351","repo":"dotnet/wpf","slug":"sr-format-sr-collection-badtype-this-gettype-name-value-7b367d","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Vector\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadType, this\\.GetType\\(\\)\\.Name, value\\.GetType\\(\\)\\.Name, \"Vector\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs","lineNumber":455,"sourceCode":"        /// </summary>\n        internal Vector Internal_GetItem(int i)\n        {\n            return _collection[i];\n        }\n\n\n\n        #endregion\n\n        #region Private Helpers\n\n        private Vector Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is Vector))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Vector\"));\n            }\n\n            return (Vector) 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(Vector 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/VectorCollection.cs#L437-L473","documentation":"VectorCollection.Cast (called from Add/Insert and other IList members) verifies every item added is a System.Windows.Vector. If the value is null-checked first and then fails the `is Vector` test, it throws ArgumentException with Collection_BadType, naming the collection type, the offending value's runtime type, and the expected element type 'Vector'. The collection is strongly typed and refuses to store anything that is not a Vector instance.","triggerScenarios":"Calling vectorCollection.Add(value) or vectorCollection.Insert(index, value) (or setting via IList indexer) where value's runtime type is not Vector - e.g. adding a Point, double, string, or a boxed numeric value.","commonSituations":"Populating a VectorCollection from XAML with wrong element types; data-binding or code that conflates Point and Vector (both have X/Y doubles); passing parsed doubles or a PointCollection's items into a VectorCollection; reflection/generic code that adds items of object type without checking.","solutions":["Ensure only Vector instances are passed to Add/Insert; convert Points with new Vector(p.X, p.Y) or p.ToVector().","Before adding, check `if (value is Vector v)` and handle/skip otherwise instead of letting the collection throw.","If building from doubles, construct Vector(x, y) explicitly rather than adding raw numbers.","Check XAML/Binding output type; a binding producing Point or double into a Vector-typed collection property triggers this."],"exampleFix":"// before\nvectorCollection.Add(myPoint); // ArgumentException: is not a Vector\n// after\nvectorCollection.Add(new Vector(myPoint.X, myPoint.Y));","handlingStrategy":"validation","validationCode":"if (value is not System.Windows.Vector)\n    throw new ArgumentException($\"{value?.GetType().Name ?? \"null\"} is not a Vector\");\nvectorCollection.Add(value);","typeGuard":"static bool IsVector(object value) => value is System.Windows.Vector;","tryCatchPattern":null,"preventionTips":["Use VectorCollection's strongly typed Add(Vector) overload rather than the IList.Add(object) path.","Convert Point/Size values to Vector explicitly at the boundary.","Validate XAML-bound data types feeding VectorCollection properties."],"tags":["wpf","argumentexception","collection","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-21T21:30:21.729Z"}