2

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

11
  • 4
    Please don't post a description of your code. Just post the relevant pieces of code. Commented Nov 15, 2016 at 22:55
  • There are lots of answers here about passing data between forms. Do some more searching without limiting yourself to lists or arrays and see if you don't find a good answer. Commented Nov 15, 2016 at 23:14
  • namespace Generic_Reconciliation_Report { public partial class ReportFormDesign : Form { public ReportFormDesign() { InitializeComponent(); } Commented Nov 15, 2016 at 23:32
  • I meant the the content of InitializeComponent(). You can find it in the Designer.cs partial class which is accessed as you click on the Form arrow: pasteboard.co/rHBzUzfHb.png Commented Nov 15, 2016 at 23:38
  • @DStanley - thanks for the comment. Not too sure if there is other way. I need to store file names and the paths so can do some check later based on individual file names. Commented Nov 15, 2016 at 23:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks David. I have realised that after added the code. once up and running I will clone the solution and use proper naming convention.
0

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.

3 Comments

thanks for all the comments. I will try all the suggestions and report back.
Great stuff. It worked and now i can access content of the array. Visual studio has complained about 'this' and asked to removed which worked. Thanks again.
I'm glad I could help. "this" is redundant for VS but some tools like Resharper recommend it as a way to know if your variable is from the class or is local for the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.