Reputation: 125
I have created a simple form in C# with few buttons and text boxes. User will enter various details such as project description and code number, output file location etc.
End user will have a browse button enabled to allow him/her to select an input directory where the reports are located.
The browse button uses FolderBrowserDialog and checks the result. If result is OK then I have created an array with paths to all the file using locationArray = Directory.GetFiles(fbd.SelectedPath, "*.txt");
The class is defined as public void.
My plan was to add some script into finish button who would then go through the array, extract file names, read the content of each file and produce report with various details.
The problem I'm having is that I cannot seem to access the array in another class(finish button).
I have defined the array before the class - string[] locationArray;
Then with the class I populate the array with file paths as per below:
locationArray = Directory.GetFiles(fbd.SelectedPath, "*.txt");
At this stage I know the array is being populated as I have displayed length of the array.
Can someone advice how do I access the array under different class so I can loop through it please.
Thanks in advance.
To be more specific my code looks like this:
string[] locationArray;
public void button1_Click_1(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Browse Directory";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
locationArray = Directory.GetFiles(fbd.SelectedPath, "*.txt");
}
FinishButton.Enabled = true;
}
trying to access locationArray in here:
private void button1_Click(object sender, EventArgs e)
{
string sProjectName = ProjectName.Text;
string sProjectNumber = ProjectNumber.Text;
string sOutputDirectory = OutputDirectory.Text;
const string message1 = "Done!";
const string caption1 = "Completed";
var result = MessageBox.Show(message1, caption1,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
if (result == DialogResult.OK)
Environment.Exit(0);
}
Hope this makes it clearer.
Tom
Upvotes: 2
Views: 177
Reputation: 528
It seems that the problem is that you click first button1
and at that time you have not initialize locationArray
. To avoid this error initialize locationArray
like that:
public ReportFormDesign()
{
InitializeComponent();
this.locationArray = new string[0];
}
Remember also that Environment.Exit()
will throw Win32Exception
sometimes.
Use instead Application.Exit()
If you exit your app every time you click button1
, you can instead of initializing locationArray
replace both lines
string test = Convert.ToString(locationArray.Length);
MessageBox.Show(test);
with
MessageBox.Show(0);
But that depends on what your intention is.
Upvotes: 0
Reputation: 2187
I think you have a bit of confusion on the terms. Those you pasted are actually methods, not classes. And if you want to share a variable between them, the best way is to create a property. So I assuming that your real class has both events... I would add something like this.
public class Form1: Form { // this should be already in your code... either form or webform
public string[] LocationArray {get; set};
public void button1_Click_1(object sender, EventArgs e)
{
this.LocationArray = ['a', 'b']; // or whatever variable
}
private void button1_Click(object sender, EventArgs e)
{
var array = this.LocationArray; // you do not need to create an extra variable, this is only a way to reference it
}
}
As additional advice free of charge: ensure you rename your objects before adding code to them, so you do not get that weird naming convention button1_Click
, but you could have btnSave_Click
that makes a lot more of sense. Accessing that same property from outside this class is also easy. If that is the case, just ping me and I can update the answer with that also.
Upvotes: 1