速览方法返回引用对象

本文关键字:引用 对象 返回 方法 | 更新日期: 2023-09-27 18:34:54

好的,所以我有一个用于打印方法的队列。 它存储文本和所选字体为我需要打印的每一行。 下面的循环应该打印出队列的内容,但它看起来像 peek 返回对象的值,而不是对对象的实际引用。 有没有办法让它返回引用?

        while (reportData.Count > 0 && checkLine(yPosition, e.MarginBounds.Bottom, reportData.Peek().selectedFont.Height))
        {
            ReportLine currentLine = reportData.Peek();
            maxCharacters = e.MarginBounds.Width / (int)currentLine.selectedFont.Size;
            if (currentLine.text.Length > maxCharacters)
            {
                e.Graphics.DrawString(currentLine.text.Substring(0, maxCharacters), currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                currentLine.text.Remove(0, maxCharacters);
            }
            else
            {
                e.Graphics.DrawString(currentLine.text, currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                reportData.Dequeue();
            }
        }

ReportLine 是一个结构,因此除非另有说明,否则它始终按值传递。 我不想将其更改为类,因为它的唯一目的是保存 2 条信息。

[编辑]

这就是ReportLine的样子。 这很简单:

public struct ReportLine
{
    public string text;
    public Font selectedFont;
}

速览方法返回引用对象

> textstring 类型的字段,您希望通过 currentLine.text.Remove(0, maxCharacters); 更改它。但是Remove不修改字符串,它会返回一个新字符串。

尝试:

currentLine.text = currentLine.text.Remove(0, maxCharacters); 

并将ReportLine设置为引用类型:

public class ReportLine
{
    public string text;
    public Font selectedFont;
}