Home > Back-end >  StreamReader as a method in different class. ( defined closed in the method but still shows being us
StreamReader as a method in different class. ( defined closed in the method but still shows being us

Time:10-06

I'm new to C# and am unfamiliar with OOP and low coupling techniques. I faced a wall where I believe that I really closed the stream reader before using it. The stream reader method is in the ReadandWriteTxtfile.

My classes that utilized StreamReader and StreamWriter: (I'll only show the significant codes)

1. MMCM Library_main

 public partial class MMCMLibrary_home : Form
    {
      
        //Instantiate the Background Colors
        TimeFrame_Colors TimeFrame = new TimeFrame_Colors();
}

  private void frmMainScreen_Load(object sender, EventArgs e)
        {
            timerlblClock.Start();
            TimeFrame.Colors();
        }
  private void frmMainScreen_Load(object sender, EventArgs e)
        {
            timerlblClock.Start();
            TimeFrame.Colors();
        }

2. ReadandWriteTxtfile

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace MMCM_Library
{
    internal class ReadandWriteTxtfile
    {
        public void Write(string filepath, string process)
        {
            StreamWriter Write = new StreamWriter(Application.StartupPath   "\\Database\\"   "\\DCRTimeFrameColors\\"   filepath);
            Write.WriteLine(process);
            Write.Close();
           
        }
        public string Read(string filepath)
        {
            StreamReader Read = new StreamReader(Application.StartupPath   "\\Database\\"   "\\DCRTimeFrameColors\\"   filepath);
            return Read.ReadLine();
            Read.Close();
        }
    }
}

3. MMCMLibrary_reserve

namespace MMCM_Library
{
    public partial class MMCMLibrary_reserve : Form
    {
        public static MMCMLibrary_reserve instance;
        int room;


        //ReadandWrite read = new ReadandWrite();
        //ReadandWrite write = new ReadandWrite();

        ReadandWriteTxtfile Writer = new ReadandWriteTxtfile();
        ReadandWriteTxtfile Reader = new ReadandWriteTxtfile();
        //TimeFrame_Colors ColorTimeFrame = new TimeFrame_Colors();

        //OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\NITRO 5\Downloads\MMCM LIbrary.accdb");
        // OleDbCommand cm = new OleDbCommand();
        //OleDbDataReader dr;
        //StreamWriter Write = new StreamWriter(Application.StartupPath   "\\Database\\"   "Reservationdata.txt");
        

        public MMCMLibrary_reserve(int roomNumber)
        {
            InitializeComponent();
            room = roomNumber;
            instance = this;
        }

        
    

        
        public void btnDCRoomsReserve_Click(object sender, EventArgs e)
        {
            //MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
            //All 9am to log if it's reserved (1 = reserved)
            if (rbtn9.Checked)
            {
                if (room == 1)
                {
                    if (Reader.Read("DCR1_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");                
                    else Writer.Write("DCR1_9am.txt", "1");                                          
                }

                else if (room == 2)
                {
                    if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
                    else Writer.Write("DCR2_9am.txt", "1");                   
                }

                else if (room == 3)
                {
                    if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
                    else Writer.Write("DCR3_9am.txt", "1");                  
                }

                else if (room == 4)
                {
                    if (Reader.Read("DCR4_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
                    else Writer.Write("DCR4_9am.txt", "1");
                }
                
           }

4. TimeFrame_Colors

namespace MMCM_Library
{
    internal class TimeFrame_Colors
    {

        public static TimeFrame_Colors instance;
        ReadandWriteTxtfile Reader = new ReadandWriteTxtfile();


        public void Colors()
        {
            instance = this;

            //DCR 1
            if (Reader.Read("DCR1_9am.txt") == "1")
            {
                MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;

            }
            if (Reader.Read("DCR1_11am.txt") == "1")
            {
                MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
            }
            if (Reader.Read("DCR1_1pm.txt") == "1")
            {
                MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
            }
            if (Reader.Read("DCR1_3pm.txt") == "1")
            {
                MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
            }

enter image description here

CodePudding user response:

The safest way to use a filestream is with a using statement.

 public void Write(string filepath, string process)
    {
      using (StreamWriter Write = new StreamWriter(Application.StartupPath   "\\Database\\"   "\\DCRTimeFrameColors\\"   filepath))
      {
        Write.WriteLine(process);
      }
    }
    public string Read(string filepath)
    {
      using (StreamReader Read = new StreamReader (Application.StartupPath   "\\Database\\"   "\\DCRTimeFrameColors\\"   filepath))
      {
        return Read.ReadLine();
      }
    }

Rather than manually calling Close at all, I'd use a using statement... but if you're just trying to write or read text to a file, use a one-shot File.WriteAllText and File.ReadAllText call:

File.WriteAllText(string filepath, string process);
File.ReadAllText(string filepath);

CodePudding user response:

StreamWriter and several other classes implement the IDisposable interface. IDisposable are generally implemented by using the following syntax:

using (StreamWriter writer = new StreamWriter(input))
{
    // work with writer is done here
}

The syntax here is intended to create a scope for an object that will eventually be garbage collected. Whatever happens in the brackets will be valid for the lifetime of the writer.

  • Related