none
StreamWriter RRS feed

  • Question

  • I'm trying to pass log to another class as follows but something is wrong and _log is null ...

    log = new StreamWriter ...
    log.WriteLine ...
    MC = new MyClass(log)

    public class MyClass
    {
      public StreamWriter _log;
      public MyClass(StreamWriter log)
      {
        _log = log;
        _log.WriteLine ...
      {
    }

    can anyone help


    • Edited by Dalai Wednesday, October 10, 2012 3:26 PM
    Wednesday, October 10, 2012 3:25 PM

Answers

  • This works fine:

       class A
       {
            private void button1_Click(object sender, EventArgs e)
            {
                StreamWriter sw = new StreamWriter(@"C:\myLog.txt");
                sw.WriteLine("abc");
                new MyClass(sw);
            }
        }
    
        public class MyClass
        {
            public StreamWriter _log;
            public MyClass(StreamWriter log)
            {
                _log = log;
                _log.WriteLine("123");
                _log.Flush(); //check now the file on c:
            }
        }


    Mitja

    • Marked as answer by Dalai Friday, October 12, 2012 11:59 AM
    Wednesday, October 10, 2012 3:34 PM

All replies

  • This works fine:

       class A
       {
            private void button1_Click(object sender, EventArgs e)
            {
                StreamWriter sw = new StreamWriter(@"C:\myLog.txt");
                sw.WriteLine("abc");
                new MyClass(sw);
            }
        }
    
        public class MyClass
        {
            public StreamWriter _log;
            public MyClass(StreamWriter log)
            {
                _log = log;
                _log.WriteLine("123");
                _log.Flush(); //check now the file on c:
            }
        }


    Mitja

    • Marked as answer by Dalai Friday, October 12, 2012 11:59 AM
    Wednesday, October 10, 2012 3:34 PM
  • Hi Dalai,

    Your code doesn't have any problem at all. May be you have corrected the issue unknowingly while posting here ;)

    Anyways, Inside MyClass constructor, did you check whether the parameter log is null? If yes, then was it null before passing it MyClass? If yes, then no question at all because the original object itslf is null. Otherwise, may be there is some piece of code which sets log to NULL befor log object is passed to MyClass.

    Check again.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, October 10, 2012 4:23 PM