How to reference a class that's been added to a C Sharp project

John500
John500 used Ask the Experts™
on
Please see the code below which includes two namespaces and two classes:

namespace GetProcessStats
{
    class ProcessInfo
    {
----------------------------

namespace GetProcessStats
{
 
    namespace AutoWriteToFile
    {
        public static class FileWriter
        {
-----------------------------------------------------------
I am having trouble accessing the FileWriter class from Main():

...
using System.IO;
using GetProcessStats.AutoWriteToFile;


class Program
    {
        static void Main(string[] args)
        {

            FileWriter FW = new FileWriter();

            ProcessInfo WscriptInfo = new ProcessInfo();

            WscriptInfo.GetProcess("D011", "mspaint");

            FW.??????   ("", "");
            
        }
    }

Open in new window


What am I missing?
using System;
using System.IO;
using System.Diagnostics;
using GetProcessStats.AutoWriteToFile;

namespace GetProcessStats
{
    class ProcessInfo
    {
        PerformanceCounter pc = new PerformanceCounter();
 
        public Process GetProcess(string hostname, string processName)
        {
            Process[] ps = Process.GetProcessesByName(processName, hostname);
 
            if (ps.Length == 0)
                return null;
            else
                return ps[0];
        }
 
        public bool IsProcessAlive(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return false;
            }
            else
            {
                return !p.HasExited;
            }
        }
 
        public string GetProcessCPU(string hostname, string processName)
        {
            string str = "";
 
            pc.CategoryName = "Process";
            pc.CounterName = "% Processor Time";
            pc.InstanceName = processName;
            pc.MachineName = hostname;
 
            try
            {
                str = pc.NextValue().ToString();
            }
            catch
            {
                str = "Error";
            }
 
            return str;
        }
 
        public string GetProcessRAM(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return "Error";
            }
            else
            {
                return p.PagedMemorySize64.ToString();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            FileWriter FW = new FileWriter();

            ProcessInfo WscriptInfo = new ProcessInfo();

            WscriptInfo.GetProcess("D011", "mspaint");

            FW. ("", "");
            
        }
    }

}
----------------------------------------------------------------------
Separate code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

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


            void WriteToFile(string FileDest, string StrOutput)
            {
                TextWriter tw;
                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)
                    {
                        Console.WriteLine(ex.Message);

                    }
                    finally
                    {
                        // close the stream
                        tw.Close();
                    }
                }
            }
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
I think the problem is that you declared the FileWriter class as a "static" class. Hence you cannot create an instance of it with "new".

Top Expert 2009

Commented:
Is there a reason you want a static class? If so, you need to call the method like so:

FileWriter.WriteToFile(...)

Commented:
WriteToFile is a private method
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
So how should it look then, like this:

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


            static void WriteToFile(string FileDest, string StrOutput)
            {
                TextWriter tw;
                if (_WriteToFile)

Open in new window

Top Expert 2009

Commented:
That isn't the point.

The point is, you made a static class, then try to instantiate it. You cannot do that.

Whether you want the methods to be private is up to you. You need a way to use the class, don't you? If so, making a static class with all private methods is useless.
Top Expert 2009

Commented:
Even with your above change, you cannot cal WriteToFile() and there are no other methods or properties. So the class isn't of much use.

So what is the purpose of the class? What do you expect the user to do with the class.

Author

Commented:
My last post results in this error:


Error      1      'GetProcessStats.AutoWriteToFile.FileWriter.WriteToFile(string, string)' is inaccessible due to its protection level

Author

Commented:
Well of course I want to use the class.  The question is how.

Commented:
Why the function isn't public?

Author

Commented:
With this change:

 public static void WriteToFile(string FileDest, string StrOutput)


I get:

Error      1      Member 'GetProcessStats.AutoWriteToFile.FileWriter.WriteToFile(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead      

For this line:

FW. WriteToFile("", "");

I thought the type was already defined by the use of:

FileWriter FW = new FileWriter();


Top Expert 2009

Commented:
>>Well of course I want to use the class.  The question is how.

By making methods that you need to call public.
Top Expert 2009
Commented:
You have 2 options:
1) Keep the class and method static, but make method public.
    Then use it like so:
        FileWriter.WriteToFile(...)
   
2) Remove static from the class, fields and methods. There is no need to have static here if you want to instantiate the class.
     Use it like so:
        FileWriter fw = new FileWriter();
        fw.WriteToFile(...);

Author

Commented:
Ok, I straightened that out with:

public void WriteToFile(string FileDest, string StrOutput)

How about one more issue related to this class.  I get this error for the 'finally' block:


Error      1      Use of unassigned local variable 'tw'      

 }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);

                    }
                    finally
                    {
                        // close the stream
                        tw.Close();            // ERROR ON THIS LINE ********************
                    }

Open in new window

Commented:
Hi, by making it static you don't need an instance, you can call it by this way:

FileWriter.WriteToFile("", "");
Top Expert 2009

Commented:
>>Error      1      Use of unassigned local variable 'tw'    

That error is very clear. You've created a variable without assigning an object to it.
Commented:
And for solving tw problem, you can do:

               TextWriter tw = null;
                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)
                    {
                        Console.WriteLine(ex.Message);

                    }
                    finally
                    {
                        // close the stream
                        if (tw ! = null)
                            tw.Close();
                    }
                }

Open in new window

Author

Commented:
Thanks guys !

Knowing what you know about my code, if you're interested, this question should be a breeze:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_27041421.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial