0

I am using this code to generate the path of a selected file:

       private void LoadNewFile()
{
    OpenFileDialog ofd = new OpenFileDialog();
    string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
    System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
    {
        userSelectedFilePath = ofd.FileName;
    }
}

        private void tbFilePath_TextChanged(object sender, EventArgs e)
        {

        }

Before i used this code to pass the data:

 private void btn_compare_Click(object sender, EventArgs e)
        {
            string x1 = System.IO.File.ReadAllText(@"C:\Users", Encoding.UTF8);

How do i modify it that instead of x1 that takes the path manually, i need it to be equal to xmlPath1 , so string x1 = xmlPath1

6
  • Please explain a bit more. Commented Sep 4, 2014 at 9:23
  • well, before i just inserted the path of the file manually as you can see, now i take it from a dialog, and i need to assign the x1 variable the path Commented Sep 4, 2014 at 9:25
  • Tell me what exactly you want to do on button click ? Commented Sep 4, 2014 at 9:36
  • I guess he wants to pass _xmlPath1 to btn_compare_Click(...) to use it there Commented Sep 4, 2014 at 9:38
  • on my btn_compare_Click the string x1 should be equal to the path i selected with the dialog opener Commented Sep 4, 2014 at 9:38

2 Answers 2

0

Just change _xmlPath1 to a field and you can access it from any method within your class.

Example:

public class MyClass
{
    protected String _xmlPath1;

    ' insert your methods here
}

If your methods are not in the same class you have to widen the scope of _xmlPath1 even more.

EDIT: Changed VB.net syntax to C#

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

2 Comments

hmm i'm not understanding where should i insert that code, my methods ar ein the same class
MyClass is just a placeholder for your real class name. Just put the line directly beneath your class definition. For further reading on this topic, have a look at this link
0

1 )If userSelectedFilePath is a private field you can use it in btn_compare_Click
2 )If it is a local variable ,make it a private field and then see 1
3)Make the the method return the file

private string LoadNewFile()
{
   OpenFileDialog ofd = new OpenFileDialog();
   string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
   System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
   if (dr == DialogResult.OK)
      return  ofd.FileName;
   else
      return null;

}

and use it (you must add validation logic)

private void btn_compare_Click(object sender, EventArgs e)
{
        string x1 = System.IO.File.ReadAllText(LoadNewFile(), Encoding.UTF8);
}

Option 3 is the best way so you can avoid the extra field and make btn_compare_Click independent from the rest of the code.
In that case you should also give it a better name like GetFileToRead()

Comments

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.