-2

I have a working project with separate classes for things like calcs, serial port, etc.. and want to encapsulate them into dll's for use in other similar projects. I'm trying to implement a factory, firstly for a serial port Factory, so I can use between multiple dlls. This is my first time and have read loads of tutorials and have started off with this example:

https://www.codeproject.com/articles/1075518/learning-factory-pattern-using-csharp

Which works nicely as it is, but I want to move the FileLogger out to it's own class, so I have been trying for some time, but need a nudge in the right direction please?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPatternSample
{

    interface ILogger
    {
        void Log(string Message);
    }

    enum LoggerType
    {
        File,
        Database,
        EventViewer
    }

    public class File 
    {
        public class FileLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in File.", Message);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPatternSample
{
    class Program
    {
        File.FileLogger flogger = new File.FileLogger();

        interface ILogger
        {
            void Log(string Message);
        }

        enum LoggerType
        {
            File,
            Database,
            EventViewer
        }



        class DatabaseLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in Database.", Message);
            }
        }

        class EventViewerLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in EventViewer.", Message);
            }
        }

        class LoggerFactory
        {
            public static ILogger Get(LoggerType type)
            {
                switch (type)
                {
                    case LoggerType.Database:
                        return new DatabaseLogger();
                    case LoggerType.EventViewer:
                        return new EventViewerLogger();
                    case LoggerType.File:
                    default:
                        return new flogger();
                }
            }
        }

        static void Main(string[] args)
        {
            ILogger logger1 = LoggerFactory.Get(LoggerType.Database);
            logger1.Log("Message from Main");

            ILogger logger2 = LoggerFactory.Get(LoggerType.File);
            logger2.Log("Message from Main");

            ILogger logger3 = LoggerFactory.Get(LoggerType.EventViewer);
            logger3.Log("Message from Main");

            Console.ReadKey();

            /*Output
            Message from Main - Logged in Database.
            Message from Main - Logged in File.
            Message from Main - Logged in EventViewer.
            */
        }
    }
}

This bit is what is causing the problem:

return new flogger();

and this is the error:

Error CS0246 The type or namespace name 'flogger' could not be found (are you missing a using directive or an assembly reference?)

I just can't get it to reference File.FileLogger, help please?

chasher
  • 149
  • 11
  • 1
    Well, it's `new File.FileLogger()` as you've already done in the line where you initialize the field: `File.FileLogger flogger = new File.FileLogger();` – Tim Schmelter Dec 07 '17 at 13:12
  • Do you want to return a new instance of `FileLogger`, or do you want to return the existing instance in your `flogger` variable? It's one or the other. – David Dec 07 '17 at 13:14
  • As Tim Schmelter said, either `return flogger;` or `return new File.FileLogger();` – Georg Patscheider Dec 07 '17 at 13:14
  • Thanks with "return flogger;" I get Error CS0120 An object reference is required for the non-static field, method, or property 'Program.flogger' – chasher Dec 07 '17 at 13:29
  • or with return new 'File.FileLogger();' I get Error CS0266 Cannot implicitly convert type 'FactoryPatternSample.File.FileLogger' to 'FactoryPatternSample.Program.ILogger'. An explicit conversion exists (are you missing a cast?) – chasher Dec 07 '17 at 13:31
  • this is what I've been fighting with these two errors.. – chasher Dec 07 '17 at 13:32

1 Answers1

0

Solved, once I removed the:

interface ILogger
{
    void Log(string Message);
}

from the program class the end went and it now works, as I Had two interfaces

chasher
  • 149
  • 11