0

さて、印刷メソッドに使用する Queue があります。印刷する必要がある各行のテキストと選択したフォントを保存します。次のループは、キューの内容を出力することになっていますが、オブジェクトへの実際の参照ではなく、オブジェクトの値を返すように見えます。参照を返すようにする方法はありますか?

        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;
}
4

1 に答える 1

3

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;
}  
于 2012-05-21T14:01:13.937 に答える