繁体   English   中英

偷看方法返回ref对象

[英]Peek method return ref object

好了,所以我有一个用于打印方法的队列。 它存储需要打印的每一行的文本和选定的字体。 下面的循环应该打印出Queue的内容,但是看起来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);更改currentLine.text.Remove(0, maxCharacters); 但是Remove不会修改字符串,它会返回一个新字符串。

尝试:

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

并使ReportLine成为引用类型:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM