dotnet/wpf · error · ArgumentException
SR.Format(SR.NegativeValue, "count")
Error message
SR.Format(SR.NegativeValue, "count")
What it means
GetTextInRun rejects a negative count with ArgumentException (NegativeValue resource). The count is the maximum number of characters to copy; a negative value has no meaning, so the method fails fast rather than treating it as zero.
Solutions
- Pass 0 (or a non-negative value) instead of negative counts; there is no -1 sentinel here.
- Fix the subtraction producing the negative remaining length.
- Clamp: count = Math.Max(0, count) before calling.
Example fix
// before pointer.GetTextInRun(dir, buffer, start, end - start); // after var count = Math.Max(0, end - start); pointer.GetTextInRun(dir, buffer, start, count);
Defensive patterns
Strategy: validation
Validate before calling
if (count < 0) count = 0; // or throw ArgumentOutOfRangeException(nameof(count))
Try / catch
try { pointer.GetTextInRun(dir, buffer, start, count); } catch (ArgumentException) { count = Math.Max(0, count); } Prevention
- Clamp count = Math.Max(0, requested).
- Guard subtraction-based counts (end - start) against end < start.
- Remember GetTextInRun has no -1 'unlimited' sentinel; pass int.MaxValue-style positives if needed.
When it happens
Trigger: Calling GetTextInRun(direction, textBuffer, startIndex, count) with count < 0, e.g. remaining-length arithmetic that went negative (buffer.Length - startIndex underflow handled separately, but expressions like end - start can yield negatives).
Common situations: Computing count as (someEnd - startIndex) where someEnd < startIndex; passing -1 as an 'unlimited' sentinel, which this API does not support.
Related errors
- SR.BadDistance
- SR.BadTextPositionOrder
- SR.Format(SR.MaxLengthExceedsBufferSize, count…
- SR.Format(SR.NegativeValue, "startIndex")
- SR.Format(SR.StartIndexExceedsBufferSize, startIndex…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/87e244f142f122ff.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs:666
/// <see cref="ITextPointer.GetTextInRun(LogicalDirection,char[],int,int)"/>
/// </summary>
/// <remarks>Only reutrn uninterrupted runs of text</remarks>
public static int GetTextInRun(DocumentSequenceTextPointer thisTp, LogicalDirection direction, char[] textBuffer, int startIndex, int count)
{
ValidationHelper.VerifyDirection(direction, "direction");
ArgumentNullException.ThrowIfNull(textBuffer);
if (startIndex < 0)
{
throw new ArgumentException(SR.Format(SR.NegativeValue, "startIndex"));
}
if (startIndex > textBuffer.Length)
{
throw new ArgumentException(SR.Format(SR.StartIndexExceedsBufferSize, startIndex, textBuffer.Length));
}
if (count < 0)
{
throw new ArgumentException(SR.Format(SR.NegativeValue, "count"));
}
if (count > textBuffer.Length - startIndex)
{
throw new ArgumentException(SR.Format(SR.MaxLengthExceedsBufferSize, count, textBuffer.Length, startIndex));
}
return thisTp.ChildPointer.GetTextInRun(direction, textBuffer, startIndex, count);
}
/// <summary>
/// <see cref="ITextPointer.GetAdjacentElement"/>
/// </summary>
/// <remarks>Return null if the embedded object does not exist</remarks>
public static object GetAdjacentElement(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
{
ValidationHelper.VerifyDirection(direction, "direction");
return xGapAwareGetEmbeddedElement(thisTp, direction);View on GitHub (pinned to 81131a70a4)