2013-04-02 47 views
0

我希望有人能幫助我。我比較新,希望瞭解如何將變量的值傳遞給另一個類。在這種情況下,我想在Button_Click_2中使用Button_Click_1中的numPage和filePath數組。我將如何引用從另一個函數中的私有函數獲取的變量?

預先感謝您!

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog(); 

     openFileDialog1.Filter = "Images (.jpg)|*.jpg"; 
     openFileDialog1.FilterIndex = 1; 

     openFileDialog1.Multiselect = true; 

     bool? userClickedOK = openFileDialog1.ShowDialog(); 

     if (userClickedOK == true) 
     { 
      string[] filePaths = openFileDialog1.FileNames; 
      int imageNum = 0; 
      lblFilePath.Content = filePaths[imageNum]; 
     } 

    } 

    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     imageNum++; 
     lblFilePath.Content = filePath[imageNum]; 
    } 
} 
+0

這是每個C#開發人員如果不是最應該知道這裏是一個MSDN網站,這將有助於您瞭解'訪問Modifiers' http://msdn.microsoft.com/en-us/library/ms173121。 aspx – MethodMan

+0

另一個有用的鏈接 - [成員變量和局部變量之間的區別是什麼](http://stackoverflow.com/questions/1177723/what-is-the-difference-between-a-member-variable-and -a局部變量)。 –

回答

3

的性能。

public partial class MainWindow : Window 
{ 
    // These can now be accessed from any method in this class. 
    private string[] filepaths = null; 
    private int imageNum = 0; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog(); 

     openFileDialog1.Filter = "Images (.jpg)|*.jpg"; 
     openFileDialog1.FilterIndex = 1; 

     openFileDialog1.Multiselect = true; 

     bool? userClickedOK = openFileDialog1.ShowDialog(); 

     if (userClickedOK == true) 
     { 
      // You can use the keyword "this" to access instance variables, but it is optional. 
      this.filePaths = openFileDialog1.FileNames; 
      this.imageNum = 0; 
      lblFilePath.Content = this.filePaths[this.imageNum]; 
     } 

    } 

    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     // You may want to put some validation in here to prevent errors situations. 

     // Validate that filePaths has been initialized. 
     if (this.filePaths == null) 
     { 
      System.Windows.Forms.MessageBox.Show("No files paths to display."); 
     } 
     // Validate that imageNum can be incremented without IndexOutOfRangeException. 
     else if (this.imageNum < this.filePaths.Length - 1) 
     { 
      this.imageNum++; 
      lblFilePath.Content = this.filePaths[this.imageNum]; 
     } 
     // Otherwise, loop back to the first file path. 
     else 
     { 
      this.imageNum = 0; 
      lblFilePath.Content = this.filePaths[this.imageNum]; 
     } 
    } 
} 
4

在MainWindow類中使用property

private string[] FilePaths{get;set;} 

用FilePath替換變量filePath。

+1

您還需要注意filePaths,如果在Button1之前單擊Button2(如果可能的話),那麼您將嘗試訪問它時會出錯。即使它在imageNum高於filePaths的上限時被初始化,你也會得到一個錯誤。 –

0

這裏需要創建實例變量,而不是作用域的變量是本地的方法:

public partial class MainWindow : Window 
{ 
    private string[] filePaths; 
    private int imageNum = 0; 

    //... 

} 

然後你就可以使用這些實例變量(確保不重新定義新的局部變量與兩種方法中的相同名稱)。

0

使這兩個,你會只需要將它們存儲爲類的實例變量,而不是本地方法變量形式1

public partial class MainWindow : Window 
{ 

    private string[] FilePaths {get;set;} 
    int imageNum = 0; 

    public MainWindow() 
    { 
    InitializeComponent(); 
    } 

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog(); 

    openFileDialog1.Filter = "Images (.jpg)|*.jpg"; 
    openFileDialog1.FilterIndex = 1; 

    openFileDialog1.Multiselect = true; 

    bool? userClickedOK = openFileDialog1.ShowDialog(); 

    if (userClickedOK == true) 
    { 
     FilePaths = openFileDialog1.FileNames; 

     lblFilePath.Content = filePaths[imageNum]; 
    } 

} 

private void Button_Click_2(object sender, RoutedEventArgs e) 
{ 
    imageNum++; 
    if(imageNum<FilePaths.Length) 
     lblFilePath.Content = FilePaths[imageNum]; 
} 

}

+0

這太好了!謝謝大家的回覆! –

相關問題