How to create a var inside of a void and then access it outside of that

Stoutscientist 21 Reputation points
2022-07-10T18:23:39.64+00:00

I have a button that when clicked opens a OpenFileDialog:
private void ImportNether_Click_1(object sender, EventArgs e)
{
OpenFileDialog mydialog = new OpenFileDialog();
mydialog.InitialDirectory = "C:";
mydialog.Title = "Open Jar File";
mydialog.Filter = "Jar files (.jar)|.jar";
mydialog.ShowDialog();
var filetorun = (new FileInfo(mydialog.FileName));
}

and was wondering how I could access that var from a different void?
(I am very new to visual studio and .net apps and am learning as I go so sorry if this is a dumb question)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,294 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
943 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points
    2022-07-10T19:35:57.547+00:00

    You would need a class level variable if you want to access in other methods

      FileInfo filetorun  = null;  
    
     private void ImportNether_Click_1(object sender, EventArgs e)  
     {  
           OpenFileDialog mydialog = new OpenFileDialog();  
           mydialog.InitialDirectory = "C:";  
           mydialog.Title = "Open Jar File";  
           mydialog.Filter = "Jar files (.jar)|.jar";  
           mydialog.ShowDialog();  
           filetorun = (new FileInfo(mydialog.FileName));  
      }  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Stoutscientist 21 Reputation points
    2022-07-10T19:40:40.923+00:00

    lol I didn't expect it to be that simple thanks!

    0 comments No comments