{"record":{"id":"4e4425ec7e32f955","repo":"dotnet/machinelearning","slug":"string-format-strings-mismatchedvaluetype-typeof-4e4425","errorCode":null,"errorMessage":"string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>))","messagePattern":"string\\.Format\\(Strings\\.MismatchedValueType, typeof\\(VBuffer<T>\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumns/VBufferDataFrameColumn.cs","lineNumber":132,"sourceCode":"                bufferIndex++;\n                bufferOffset = 0;\n            }\n            return ret;\n        }\n\n        protected override void SetValue(long rowIndex, object value)\n        {\n            if (value == null)\n            {\n                throw new NotSupportedException(\"Null values are not supported by VBufferDataFrameColumn\");\n            }\n            else if (value is VBuffer<T> vbuffer)\n            {\n                SetTypedValue(rowIndex, vbuffer);\n            }\n            else\n            {\n                throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>)), nameof(value));\n            }\n        }\n\n        protected void SetTypedValue(long rowIndex, VBuffer<T> value)\n        {\n            int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);\n            _vBuffers[bufferIndex][(int)(rowIndex % MaxCapacity)] = value;\n        }\n\n        public new VBuffer<T> this[long rowIndex]\n        {\n            get => GetTypedValue(rowIndex);\n            set => SetTypedValue(rowIndex, value);\n        }\n\n        /// <summary>\n        /// Returns an enumerator that iterates through the VBuffer values in this column.\n        /// </summary>","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumns/VBufferDataFrameColumn.cs#L114-L150","documentation":"VBufferDataFrameColumn<T>.SetValue only accepts values that are exactly VBuffer<T>; anything else (including null or a plain T) throws ArgumentException formatted from Strings.MismatchedValueType, with 'value' as the paramName. The column's storage is buffer-based and cannot coerce arbitrary objects into VBuffer<T>.","triggerScenarios":"Calling SetValue(rowIndex, obj) (directly or via DataFrame indexing) on a VBufferDataFrameColumn<T> with a value that is not an instance of VBuffer<T> — e.g. a plain T[], a scalar T, or a null boxed in object.","commonSituations":"Populating columns via reflection or object arrays where element types degrade to object; mixing T and VBuffer<T> columns; passing raw values read from an IDataView without type checks.","solutions":["Wrap the value in a VBuffer<T> before calling SetValue (new VBuffer<T>(length, values) or VBuffer.Editor).","Verify the runtime type with 'value is VBuffer<T> v' before the call and handle the else branch.","Ensure the buffer's element type matches the column's T; a VBuffer<U> with U != T fails the pattern match.","Convert dense data with VBuffer.CreateDense(length, values)."],"exampleFix":"// before\ncolumn.SetValue(rowIndex, myArray); // myArray is T[]\n// after\nif (myArray is VBuffer<T> v)\n    column.SetValue(rowIndex, v);\nelse\n    column.SetValue(rowIndex, VBuffer.CreateDense(myArray.Length, myArray));","handlingStrategy":"type-guard","validationCode":"bool ok = value is VBuffer<T>;\nif (!ok) throw new ArgumentException($\"Expected VBuffer<{typeof(T).Name}>, got {value?.GetType().Name ?? \"null\"}\", nameof(value));","typeGuard":"static bool IsVBuffer<T>(object value) => value is VBuffer<T>;","tryCatchPattern":"try { column.SetValue(rowIndex, value); }\ncatch (ArgumentException ex) when (ex.ParamName == \"value\")\n{ /* value was not VBuffer<T>: convert or log */ }","preventionTips":["Keep column values strongly typed as VBuffer<T> end to end.","Avoid object intermediaries when filling DataFrame columns.","Pattern-match (is VBuffer<T>) before every dynamic assignment.","Convert dense arrays with VBuffer.CreateDense as a standard step."],"tags":["argument-exception","type-mismatch","dataframe","vbuffer"],"backgroundTag":"type-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}