jeudi 7 décembre 2017

Abstract Factory Error CS0246 C# .NET

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:

http://ift.tt/2j1Yml9

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?

Aucun commentaire:

Enregistrer un commentaire