{"record":{"id":"bc89230a2e9dc21e","repo":"dotnet/machinelearning","slug":"string-format-strings-mismatchedvaluetype-typeof","errorCode":null,"errorMessage":"string.Format(Strings.MismatchedValueType, typeof(string))","messagePattern":"string\\.Format\\(Strings\\.MismatchedValueType, typeof\\(string\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumns/StringDataFrameColumn.cs","lineNumber":153,"sourceCode":"        protected override void SetValue(long rowIndex, object value)\n        {\n            if (value == null || value is string)\n            {\n                int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);\n                int bufferOffset = (int)(rowIndex % MaxCapacity);\n                var oldValue = this[rowIndex];\n                _stringBuffers[bufferIndex][bufferOffset] = (string)value;\n                if (oldValue != (string)value)\n                {\n                    if (value == null)\n                        _nullCount++;\n                    if (oldValue == null && _nullCount > 0)\n                        _nullCount--;\n                }\n            }\n            else\n            {\n                throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(string)), nameof(value));\n            }\n        }\n\n        public new string this[long rowIndex]\n        {\n            get => (string)GetValue(rowIndex);\n            set => SetValue(rowIndex, value);\n        }\n\n        public new List<string> this[long startIndex, int length]\n        {\n            get\n            {\n                var ret = new List<string>();\n                int bufferIndex = GetBufferIndexContainingRowIndex(startIndex);\n                int bufferOffset = (int)(startIndex % MaxCapacity);\n                while (ret.Count < length && bufferIndex < _stringBuffers.Count)\n                {","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumns/StringDataFrameColumn.cs#L135-L171","documentation":"StringDataFrameColumn.SetValue (and the object indexer) requires the assigned value to be a string. Passing any other object type throws ArgumentException formatted with Strings.MismatchedValueType naming typeof(string).","triggerScenarios":"Assigning a non-string (e.g. int, DateTime, null-typed object) via the non-generic this[long] object indexer or SetValue(rowIndex, value).","commonSituations":"Copying values from a loosely-typed row into a string column; boxing numeric values into the object indexer; dynamic/JSON data with unexpected element types.","solutions":["Convert the value to a string before assignment (e.g. .ToString() or Convert.ToString)","Use the strongly typed string indexer column[rowIndex] = someString","Validate value is string before calling SetValue","Check the source column's DataType before copying values across columns"],"exampleFix":"// before\ncolumn[rowIndex] = 42;\n// after\ncolumn[rowIndex] = 42.ToString();","handlingStrategy":"type-guard","validationCode":"if (value is not string s) throw new ArgumentException($\"Expected string, got {value?.GetType().Name}\");","typeGuard":"bool IsStringValue(object v) => v is string;","tryCatchPattern":"try { column[rowIndex] = value; } catch (ArgumentException e) { /* value type mismatch; convert and retry */ }","preventionTips":["Use the strongly typed string indexer instead of the object indexer","Convert with ToString() at the boundary when ingesting dynamic data","Check source column DataType before cross-column copies"],"tags":["type-mismatch","argumentexception","dataframe"],"backgroundTag":"type-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}