Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

Declaring a class for use in multiple code files

Greetings,

I'm using the following code in a separate file to declare a namespace for writing output files:

using System;
using System.IO;

namespace AutoWriteToFile
{
    public class FileWriter
    {
        // Set this to false to stop writing to file
        private static bool _WriteToFile = true;
        private TextWriter tw;

        void WriteToFile(string FileDest, string StrOutput)
        {

            if (_WriteToFile)
            {                

                try
                {
                    // create a writer and open the file
                    tw = new StreamWriter(FileDest);

                    // write a line of text to the file
                    tw.WriteLine(StrOutput);
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
                finally
                {
                    // close the stream
                    tw.Close();
                }
            }
        }
    }
}

My problem is related to declaring the class in other files.  For instance, if I want to use this routine in Form1 I would declare the namespace:

using ....
using AutoWriteToFile;

... But how should I declare the class in the Form1 class so I can access the WriteToFile() function?

public partial class Form1 : Form
    {

        public FileWriter FW = new FileWriter();   ????????????????
       
        public Form1()
        {
               
                InitializeComponent();
        }

        ...
        ...
        public void FormCreate(object sender, EventArgs e)
        {
            FW. ???????  This is not working

        ...
        ...

Thank you!
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You almost have it.

namespace AutoWriteToFile
{
    public class FileWriter
    {
      public FileWriter() {};
Avatar of John500

ASKER

Andy,

Thanks for the input.  I tried inserting exactly how you suggested but it won't compile.  What am I missing?  Can you include the rest of my code as it should appear after the:

public FileWriter() {};

Thanks
Avatar of John500

ASKER

I took out the semicolon and it compiled but within Form1, I don't get access to the function WriteToFile() when I type FW..

This code is being used in VS 2008 and has worked before.  I'm not even sure why the line you mentioned is necessary.  Please advise, thank you !
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John500

ASKER

Thank you
Aha - I thought you had a problem with not being able to instance the class (for some reason I thought it generated a default private constructor hence my suggestion to make a public constructor).


public partial class Form1 : Form
    {

        public FileWriter FW = new FileWriter();   ????????????????
@AndyAinscow

But that declares a publicly-accessible field in Form1. Ordinarily, one wouldn't want to expose a field as public.
@kaufmed - I agree with you about coding styles re member variables and accessibility BUT I thought the asker meant he was getting an error at that line (which would result in not being able to use any functions in the class).
ps.  I was unavailable from posting my first comment until after the question was closed, I'm just pointing out that the question didn't clearly state where the error was.  I took the row of query signs as a hint to the faulting line.
@AndyAinscow

I didn't realize you were quoting the OP. My mistake : )