Solved

VS 2013 - Factory Pattern - FileReaderFactory

Posted on 2015-02-22
7
66 Views
Last Modified: 2015-03-09
I am working on practicing factory patterns.  I am implementing sample code.  I am having issue with the below line of code.

IFileReader objFileReader = FileReaderFactory.LoadFile(this.txtFile.Text);

Open in new window


The error I am receiving is:  The name 'FileReaderFactory' does not exist in the current context.

Any idea how to resolve?
0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40624713
Have you imported the correct namespace? What pops up if you set the cursor to IFileReader and then hit Ctrl-period? Are you offered any namespaces? If not, then you may need to add a reference to a DLL.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 40624867
Here is my code in its entirety

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryPattern
{
    public partial class FPForm : Form
    {
        public FPForm()
        {
            InitializeComponent();
        }

        private void FPForm_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }

        private void btnFileReaderFactory_Click(object sender, EventArgs e)
        {
            if (this.txtFile.Text == "")
            {
                MessageBox.Show("Please enter file name");
                return;
            }
            IFileReader objFileReader = FileReaderFactory.LoadFile(this.txtFile.Text);
            MessageBox.Show(objFileReader.FileReader()); 
        }

        private void btnRecursion_Click(object sender, EventArgs e)
        {
            DirSearch(this.textBox1.Text);
        }

        private void DirSearch(string Dir)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(Dir))
                {
                    foreach (string f in Directory.GetFiles(d))
                    {
                        textBox2.Text = textBox2.Text + f + "\n";
                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        } 
    }
}

Open in new window


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

namespace FactoryPattern
{
    interface IFileReader
    {
        string FileReader();
    }
}

Open in new window


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

namespace FactoryPattern
{
    public class ExcelFileReader : IFileReader
    {
        #region IFileReader Members

        string file = "";
        public ExcelFileReader(string fileName)
        {
            file = fileName;
        }

        public string FileReader()
        {
            return "Reading Excel Fele " + file;
        }

        #endregion
    }
}

Open in new window


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

namespace FactoryPattern
{
    public class TextFileReader : IFileReader
    {
        #region IFileReader Members

        string file = "";
        public TextFileReader(string fileName)
        {
            file = fileName;
        }
        public string FileReader()
        {
            return "Reading Text File " + file;
        }

        #endregion 
    }
}

Open in new window


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

namespace FactoryPattern
{
    class XMLFileReader
    {
        #region IFileReader Members

        string file = ""; 
        public XMLFileReader(string fileName) 
        { 
            file = fileName; 
        } 
        public string FileReader() 
        { 
            return "Reading XML File " + file; 
        } 
        #endregion 
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 40624869
BTW - Nothing happens if I hit Ctrl + period on iFileReader
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 40624932
Your code isn't complete. Is this homework? It almost seems like homework. We can't do your homework, but here is what I see that's wrong:

1. First off, your Interface definition is wonky. Each of your reader classes describes two methods in the "interface region", but only one method is actually defined in the actual interface:
   a. ExcelFileReader, FileReader.
   b. TextReader, FileReader.
   c. XMLReader, FileReader.
... the Interface itself only describes FileReader. What's your intent here? I suspect your intent is only to provide an Interface for FileReader and the other readers are internal to the classes. When you call FileReader you really call TextReader for a text file.

2. Secondly, I think you've only partially implemented the Factory. What you want is the single Load method to take the file type and load the correct file based on the type, right?
   a. You're missing the definition for the .Load(...) method.
   b. You probably need logic that determines the file type and calls the appropriate actual reader (Excel, Text, and XML).
        i. Maybe check the extension, if .xml, then use the XMLReader, etc.

Kind of the gist of what a factory pattern does ,right? You don't say "LoadExcelFile" you just say "Load" and it automagically loads the proper file type. But all that has to actually be implemented. It won't just happen. You need to write the Load method, etc.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40624939
Most likely you need to mark your interface definition as public. Types without an explicit accesibility modifer default to internal. If this is all in the same project, then it should not matter, but if you have multiple projects, then you need to set your access modifiers appropriately.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 40624964
It's not homework.  I'm just trying to write a factory pattern on my own.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 40655292
@Dan7el.  You were right.  Code was not complete.  A friend of mine sent me the code.  He forgot to send me one class.  When I added the class it fixed the solution.
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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