views:

92

answers:

3

Ok guys, this is very basic stuff I think.

I'm only getting familiar with C# and object oriented languages.

So here is a basic project I'm developping :

namespace MyProject  
{  
    class MyProject  
    {
        public Logger Logs = new Logger();
        public WebServer Web = new WebServer();

        static void Main()
        {  
            // blabla
        }  
    }  
}

The logger is there to simply create a log file and when its "Add(string str)" method is called, it adds a line in the log file. The webserver is my own webserver, opening a socket etc ... I'd like my webserver to be able to use my Add method of the logger... how am I supposed to achieve that ?

Inside my webserver class, using something like "MyProject.Logs.Add(string str)" doesn't seem to work. So how can I reference it ? I don't want to create a new instance of my Logger class inside my webserver, because I want to write everyting in the same text file (I might have more module running than just a webserver as well). So I'd like to use the instance I created in my Main method, inside my Webserver class...

I hope this makes sense ...

+4  A: 

A couple of suggestions:

  1. Logs and Web should be private. Those are implementation details of your class. They're nobody else's business.
  2. Pass the instance of your logger to your web server:

    public WebServer Web = new WebServer(Logs); 
    

Your web server can now have its very own copy:

private Logger Logs {get;set;}

public WebServer(Logger logs)
{
    Logs = logs;
}

public void DoSomething()
{
    Logs.Add("I did something!");
}
John Saunders
Thanks for your quick answer, and yes, I realise Logs and Web should be private, I put them public hoping to be able to access them that way, but I was wrong :)
Ben
+1  A: 

You can do this easily by passing a reference to the Logger instance you created in within MyProject to your instance of WebServer.

In your WebServer class, you can make a property (a getter/setter) of type Logger, so that WebServer knows about the Logger you want to use. Another alternative (probably better) is to add a parameter to the constructor of WebServer that takes the Logger. Pseudo code for adding the property:

public class WebServer
{
    public WebServer(Logger l)
    {
        Log = l;
    }

    //relevant code....
    public Logger Log { get; set; } //Short hand property declaration, see http://msdn.microsoft.com/en-us/library/bb384054.aspx
}

From inside the WebServer class, you can use the logger like Logger.Add("some text..."); Then, in your Main method, you can pass in your instance of Logger like so:

namespace MyProject  
{  
    class MyProject  
    {
        public static Logger Logs = new Logger();
        public static WebServer Web;

        static void Main()
        {  
            Web = new WebServer(Logs);
        }  
    }  
}
wsanville
This will not compile. You are accessing instance members from static method.
Rotsor
Good call, copied from the OP. That's what I get for typing in the editor and not VS.
wsanville
Thanks, it's pretty much what John Saunders says, I appreciate the help tho :)
Ben
+4  A: 

MyProject.Logs.Add(string str) does not work because Logs member is not static, which means it can be different for different instances of your class (which are created by operator new). The compiler can't know which instance you are referring to if you don't specify it.

If you mark Logs as static, then it will work.

However, making it static makes your project less customizable (for example you will not be able to start two web-servers with two different log-files in the same program). That's why what John Saunders suggested is usually considered better.

Rotsor
Interesting.Like you said, I think I'm gonna go with what John said, it does indeed offer more flexibility than making these static.
Ben