-2

How to use a same StreamWriter across various methods in a class. For eg.

public class XMLWriter
{       
  public export(string filename)       
  {
    StreamWriter sw = new StreamWriter(filename)
    sw.write("Line1")       
  }       

  public footer()       
  {
    // Note: I am not declaring streamwriter here since i want to use the same sw as in export method
    sw.write("Line x N")
  }                       
}

How can I use the same sw across many methods. Also this class will be instantiated from another class and the "public" methods will be called from there.

Any help will be highly appreciated.

vrajs5
  • 4,066
  • 1
  • 27
  • 44
WoodyStory
  • 471
  • 1
  • 4
  • 9

4 Answers4

0

I would just declare thee steamwriter above the methods (global variable) and do the work inside the methods

JoJo
  • 806
  • 1
  • 13
  • 26
0

Declare sw as a global variable, and only close and dispose it when you dispose your XMLWriter object (or when you know you won't write more into your file) with calling the DisposeWriter() method below from the class where you created that object:

public class MyClass
{
    private void DoSomeStuff()
    {
        XMLWriter xmlwr = new XMLWriter();
        xmlwr.export(@"C:\YourFile.txt");
        xmlwr.footer();
        xmlwr.DisposeWriter();
        wmlwr = null;
    }
}

public class XMLWriter
    {       
        private StreamWriter sw;

        public XMLWriter()
        {
            //this is the constructor, what you call with "new XMLWriter()"
        }

        public void export(string filename)       
        {
            sw = new StreamWriter(filename)
            sw.write("Line1")     
        }       

        public void footer()       
        {
            sw.write("Line x N")
        } 

        public void DisposeWriter()
        {
            sw.Close();
            sw.Dispose();
        }            
    }
Richtenzorg
  • 178
  • 1
  • 1
  • 9
-1

Pass it as parameter or use a private field - depends on you requirements.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76
-3
using System;
using System.IO;
using System.Text;

public class XMLWriter
{
    //Objs
    private StreamWriter sw;
    private StringBuilder sb;
    //static items
    private string strHeader;
    private string strFooter;

    public XMLWriter()
    {
        //this is the constructor, what you call with "new XMLWriter()"
    }

    public void export(string filename)       
    {
        sb = new StringBuilder();
        sw = new StreamWriter(filename);
        sw.Write(strHeader + sb.ToString() + strFooter);
        sw.Close();
        sw.Dispose();
    }

    public string Footer
    {
        set
        {
            strFooter = value;
        }
    }

    public string Header
    {
        set
        {
            strHeader = value;
        }
    }

    public string LinesAdd
    {
        set
        {
            sb.Append(value);
        }
    }

}
Anthony Horne
  • 2,522
  • 2
  • 29
  • 51
  • ref? Why? ref is almost never required. – Sriram Sakthivel Apr 27 '14 at 08:33
  • The thing is I am creating a XML class, which will create a XML output file. wherein the initial header and the footer will be static. But the actual data the rows/ columns will be dynamic. So those values has to be passed separately. In short, first the sw will dump all the header in a file using the sw, then the other class will pass the columns/ rows which has to be written to the file, finally a static footer will be written to the file. If you are aware of any better process please suggest. – WoodyStory Apr 27 '14 at 08:38
  • If Zoolway wants to use the same object, not a copy if, then he must use ref. Using your method will not persist changes between function calls, as he asked. Also, if he really chooses to use the stream writer without closing, there will be file locking issues. – Anthony Horne Apr 27 '14 at 08:41
  • How much data are we talking? – Anthony Horne Apr 27 '14 at 08:43
  • If it us not too much data, I would put it in a stringbuilder and the write it out all at once. Should be much quicker too. – Anthony Horne Apr 27 '14 at 08:48
  • `ref` is not needed or makes sense. This is C#. The streamwriter is passed, not a copy of it. The compiler even could not make a copy of it without additional work... – ZoolWay Apr 27 '14 at 11:06
  • @user3549975 Firstly, I did not down-vote your question, secondly some people are just frustrated and want to troll, and thirdly, I have updated you answer to do what I think you are looking for. I do still believe that using the stringwriter (and not hanging onto it) is the answer. The above is reasonable efficient and should not cause any resource issues. The header and footer can be set, but the lines will be appended every time it is called. Holler if you still need help. – Anthony Horne Apr 27 '14 at 19:37
  • @user3549975 Does my answer work as a solution for you? – Anthony Horne Apr 29 '14 at 09:08