title: Static Methods and ability to access variables
body: <p>I am trying to access variables from a static Method.  I understand the basic concept of static vs non static methods, but still do not fully understand what a static method can and cannot access.</p>
  
  <p>At first I tried to put my variables at the top in the Program Class as you can see from some of my commented out lines.  I could not access them from my static method which as I learned more about static made sense.  But then I put the variables inside the static method. And this is what I do not understand.  Inside the static method I can set <code>fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);</code> yet once outside the if statement the  <code>sw = new StreamWriter(fs)</code> gave me an error when I had <code>FileStream fs</code> inside the static method.  Tried making it static as you can see by the commented out line inside the static method but that does not work either.  The only way I seem to be able to access my variables is if I make them static inside the Program class (outside the static method).</p>
  
  <pre><code>class Program {
      static FileStream fs;
      //   public bool fileopen = false;
      //  String filename = @"D:\Stock Trading\Test1.txt";
      static void Main(string[] args) {
          CreateDebugFile();
      }
  
      public static void CreateDebugFile() {
          StreamWriter sw;
          bool fileopen = false;
          //      static FileStream fs;
          String filename = @
          "D:\Stock Trading\Test1.txt";
  
          if (!fileopen) {
              fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
              fileopen = true;
          }
  
          sw = new StreamWriter(fs);
          sw.Write("Current Bar" + "\t" + "time" + "\t" + "Open" + "\t" + "Close" + "\t" + "Low" + "\t" + "High");
  
      }
  }
  </code></pre>