好的,我有一个队列,用于我的print方法。它为我需要打印的每一行存储文本和选定的字体。下面的循环应该打印出队列的内容,但是看起来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是一个结构,所以除非另有说明,否则它总是通过值传递。我不想将它更改为一个类,因为它的唯一目的是保存两条信息。
编辑
这就是ReportLine的样子。这很简单:
public struct ReportLine
{
public string text;
public Font selectedFont;
}发布于 2012-05-21 22:01:14
text是一个string类型的字段,您希望currentLine.text.Remove(0, maxCharacters);对其进行更改。但是Remove不修改字符串,它返回一个新的字符串。
尝试:
currentLine.text = currentLine.text.Remove(0, maxCharacters); 并将ReportLine设置为引用类型:
public class ReportLine
{
public string text;
public Font selectedFont;
} https://stackoverflow.com/questions/10686595
复制相似问题