2015-02-11 110 views
-3

如何訪問foreach循環中的變量name以及下面的try?我需要在我的Main類中引用它。對不起,這是一個愚蠢的問題。從類內深處訪問變量

public class DragDropRichTextBox : RichTextBox 
    { 

    public DragDropRichTextBox() 
    { 
     //Enables drag and drop on this class. 
     this.AllowDrop = true; 
     this.DragDrop += DragDropRichTextBox_DragDrop; 
    } 

    public void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e) 
    {   

     string[] _fileText; 
     _fileText = e.Data.GetData(DataFormats.FileDrop) as string[]; 


     if (_fileText != null) 
     { 
      foreach (string name in _fileText) 
      { 
       try 
       { 
        this.AppendText(BinaryFile.ReadString(name)); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
     } 
    } 

這裏就是我需要從我的主類調用它(請參閱 「HERE」 下面):

private void button5_Click(object sender, EventArgs e) 
    { 
     { 
      PrintDialog pd = new PrintDialog(); 
      pd.PrinterSettings = new PrinterSettings(); 
      if (DialogResult.OK == pd.ShowDialog(this)) 
      { 
       RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, HERE); 
      } 
     } 
    } 
+2

目前還不清楚你的第一堂課和你的主課是如何相互關聯的。你在一個循環中有'name',你可以有多個值,那麼你是否要求所有的值,第一個值,最後一個值?需要更多信息。 – 2015-02-11 17:53:11

回答

4

平原和簡單,你絕對不能

該變量的範圍爲foreach循環。沒有辦法在循環之外訪問它,因爲甚至不存在以外的循環。

沒有循環,它的作用域爲try塊,沒有這個方法。即使在這些情況下,變量不在其範圍之外。

即使它有,它會保持什麼價值?它是一個迭代變量,所以它在循環的每一遍都會變化。整件事情沒有意義。

如果您需要訪問指向/保存的數據,則需要將其存儲在級變量變量中,或者在這種情況下,將每個值放入IEnumerable<String>

+0

是否可以使用'DragDropRichTextBox_DragDrop._fileText'然後重新創建'foreach'循環?我不知道如何從另一個類訪問'_fileText'變量,因爲它基於一個事件(即DragDrop)。 – Gernatch 2015-02-11 19:22:07

+0

對不起,讓我先嚐試一下你已經提出的建議。我很感激幫助。謝謝。 – Gernatch 2015-02-11 19:25:49

0

在這裏進入一些奇怪的東西,但我只是回答你的問題,並留在那。只要向上看,你所指的(名稱)會在你通過foreach循環時不斷改變值。

public class DragDropRichTextBox : RichTextBox 
{ 
    public static List<string> NameList; // Create instance variable 

    public DragDropRichTextBox() 
    { 
     //Enables drag and drop on this class. 
     this.AllowDrop = true; 
     this.DragDrop += DragDropRichTextBox_DragDrop; 
    } 

    public void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e) 
     { 

      string[] _fileText; 
      _fileText = e.Data.GetData(DataFormats.FileDrop) as string[]; 


      if (_fileText != null) 
      { 
       foreach (string name in _fileText) 
       { 
        try 
        { 
         this.AppendText(BinaryFile.ReadString(name); 
         NameList.Add(name); // Assign it inside loop 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message); 
        } 
       } 
      } 
     }  
} 

private void button5_Click(object sender, EventArgs e) 
    { 
      PrintDialog pd = new PrintDialog(); 
      pd.PrinterSettings = new PrinterSettings(); 
      if (DialogResult.OK == pd.ShowDialog(this)) 
      {       
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, DragDropRichTextBox.Name); 
      } 
    } 
+2

這是如何回答他的問題?變量範圍並不是那麼奇怪,而且非常重要。 – BradleyDotNET 2015-02-11 18:09:26

+0

他要求'名稱',這給了他。我從來沒有說範圍是奇怪的(我同意它的重要理解),他的問題和編碼風格很奇怪。無論如何,我回答了他提出的問題,同時也提到他所指的「名稱」會在遍歷foreach循環時發生變化。 – Justin 2015-02-11 18:54:30

+1

你剛纔說「陷入一些奇怪的東西」讓我感到困惑。考慮突出你的改變,因爲它不是簡單地通過代碼掃描。 (不是DV btw) – BradleyDotNET 2015-02-11 18:57:08