Solved

Declaring a class for use in multiple code files

Posted on 2013-06-24
10
346 Views
Last Modified: 2013-06-24
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!
0
Comment
Question by:John500
  • 4
  • 3
  • 3
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39272281
You almost have it.

namespace AutoWriteToFile
{
    public class FileWriter
    {
      public FileWriter() {};
0
 

Author Comment

by:John500
ID: 39272296
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
0
 

Author Comment

by:John500
ID: 39272325
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 !
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39272337
You didn't specify an accessor on the WriteToFile method. This means, in C#, that it defaults to internal, which cannot be accessed outside of the assembly which contains the method definition. If you are still within the same assembly (i.e. same project), then that should be fine. However, if you put this code into its own project which the form project references, then you will need to change your method definition to:

...

public void WriteToFile(string FileDest, string StrOutput)
{
...

Open in new window

0
 

Author Closing Comment

by:John500
ID: 39272364
Thank you
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39272472
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();   ????????????????
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39272540
@AndyAinscow

But that declares a publicly-accessible field in Form1. Ordinarily, one wouldn't want to expose a field as public.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39272583
@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).
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39272592
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39272641
@AndyAinscow

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

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 28
User Authentication using Digital Certificate 2 46
Hey!!!! 1 20
How to parse the string and split the sub directory names in asp.net 7 41
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question