2011-03-23 101 views
0

OO很新,所以請客氣。用兩種方法引用StreamReader

我已經創建了一個方法,當點擊button1時,打開文件對話框並將內容讀入流讀取器sr;

public void button1_Click(object sender, EventArgs e) 
    { 
     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      label1.Text = openFileDialog1.FileName; 

      StreamReader sr = new StreamReader(label1.Text); 
      String strNumVertices = sr.ReadLine(); 
      label2.Text = strNumVertices; 
     } 
    } 

其他代碼在Form1_Paint方法中運行。

public void Form1_Paint(object sender, PaintEventArgs e) 

     perspectiveMatrix = new Gmatrix("perspective"); 
     translationMatrix = new Gmatrix("translation"); 
     scalingMatrix = new Gmatrix("scaling"); 

     perspectiveMatrix.initAsPerspectiveMatrix(300); 

     scalingMatrix.initAsScalingMatrix(10, 10, 10); 

     translationMatrix.initAsTranslationMatrix(150, 50, 1200); 

     String strNumVertices = sr.ReadLine(); 
     label1.Text = strNumVertices; 

我的問題是,如何從Form1_paint方法中的button1_click方法引用流讀取器sr?

回答

4

建議的話 - 不要嘗試。

如果你這樣做,你有在所有地方打開文件/流的危險。

我建議你在每種方法中打開一個新的蒸汽讀取器(或將其抽象成它自己的方法)。

注:

你應該換流開幕的using聲明,以確保正確處置:

using(StreamReader sr = new StreamReader(label1.Text)) 
{ 
    String strNumVertices = sr.ReadLine(); 
    label2.Text = strNumVertices; 
} 
0

爲什麼不只是存儲你從文件中讀取數據,並重新使用它而不是再次讀取文件?我假設這些方法是在同一個類(和對象的同一個實例)中,但是如果這不是真的,那麼有辦法解決這個問題。

private string StrNumVertices { get; set; } 


public void button1_Click(object sender, EventArgs e) 
{ 
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    { 
     label1.Text = openFileDialog1.FileName; 

     StreamReader sr = new StreamReader(label1.Text); 
     this.StrNumVertices = sr.ReadLine(); 
     label2.Text = this.StrNumVertices; 
    } 
} 

public void Form1_Paint(object sender, PaintEventArgs e) 

    perspectiveMatrix = new Gmatrix("perspective"); 
    translationMatrix = new Gmatrix("translation"); 
    scalingMatrix = new Gmatrix("scaling"); 

    perspectiveMatrix.initAsPerspectiveMatrix(300); 

    scalingMatrix.initAsScalingMatrix(10, 10, 10); 

    translationMatrix.initAsTranslationMatrix(150, 50, 1200); 

    label1.Text = this.StrNumVertices; 

    ... 
} 

如果不是對象的同一實例,那麼我會考慮使用一個Singleton配置對象(或高速緩存),並存儲數據在那裏。當然,這取決於數據的範圍和生命週期 - 它是否適用於整個應用程序或僅適用於此實例?當然,最好的方法是使它成爲像上面那樣的實例屬性,並且我認爲這會起作用,但是如果要重新創建對象,則必須使用其他技術。

如果您確實想再次讀取文件(因爲數據來自不同的行),您將需要重新使用該流或再次讀取所有數據 - 如果可能的話 - 然後遍歷您在內部讀取的項目。

0

使其成爲窗體類中的字段。這將範圍從方法更改爲整個表單。上一篇文章的警告仍然有效。

StreamReader sr; 
public void button1_Click(object sender, EventArgs e) 
{ 
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    { 
     label1.Text = openFileDialog1.FileName; 

     sr = new StreamReader(label1.Text); 
     String strNumVertices = sr.ReadLine(); 
     label2.Text = strNumVertices; 
    } 
} 

public void Form1_Paint(object sender, PaintEventArgs e) 

    perspectiveMatrix = new Gmatrix("perspective"); 
    translationMatrix = new Gmatrix("translation"); 
    scalingMatrix = new Gmatrix("scaling"); 

    perspectiveMatrix.initAsPerspectiveMatrix(300); 

    scalingMatrix.initAsScalingMatrix(10, 10, 10); 

    translationMatrix.initAsTranslationMatrix(150, 50, 1200); 

    if (sr != null) { 
     String strNumVertices = sr.ReadLine(); 
     label1.Text = strNumVertices; 
    } 
+0

雖然這確實回答了這個問題,但我會質疑這種方法 - 1.在每個繪畫中留下一個打開的流2.ReadLine()將會非常緩慢3。當文件到達它將炸燬 – 2011-03-23 12:21:39

+0

感謝您的回覆... – Gary 2011-03-23 13:02:55

+0

您建議的代碼,雖然它代表我正在尋找的解決方案,但它不會在Form1_Paint方法中使用strNumVertices的值填充label1 ,這是爲什麼呢? – Gary 2011-03-23 13:09:22

1

實際上,在每次噴漆過程中從流中讀取數據並不是最好的方法。也許你想讀一次這個值,將它存儲在你的表單的成員變量中,然後訪問paint方法?

+1

哈哈,你打我一拳因爲我的老闆過來聊天。你有足夠的代表,繼續,刪除你的答案,讓我有qudos。 ;) – BenCr 2011-03-23 12:15:21

+0

@BenCr:我已經把你的upvoted,反正我們都寫基本相同:) – Vlad 2011-03-23 12:27:03

1

除非您希望文件在按鈕點擊和被調用的paint方法之間發生了變化,否則您不應該再次從該文件中讀取文件。

與將結果存儲在字段中並在繪製方法中將其撤回時相比,讀取文件在性能方面非常昂貴。或者取決於先執行什麼。