Reputation: 871
I'm hoping someone can help me. I am relatively new and would like to understand how to pass the value of the variable in one class to another. In this case I would like to use numPage and filePath array from Button_Click_1 in Button_Click_2.
Thank you in advance!
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];
}
}
Upvotes: 1
Views: 1472
Reputation: 3265
You would just need to store them as class instance variables instead of local method variables.
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];
}
}
}
Upvotes: 3
Reputation: 17556
make both as properties of form 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];
}
}
Upvotes: 0
Reputation: 203837
You need to create instance variables here, rather than scoping the variables to be local to the method:
public partial class MainWindow : Window
{
private string[] filePaths;
private int imageNum = 0;
//...
}
Then you can use those instance variables (make sure to not re-define new local variables with the same name) in the two methods.
Upvotes: 0
Reputation: 11744
Use a property in class MainWindow.
private string[] FilePaths{get;set;}
Replace variable filePath with FilePath.
Upvotes: 4