Solved

VS 2013 - Factory Pattern - FileReaderFactory

Posted on 2015-02-22
7
61 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 30
Cant save 3D 4 19
VB: Convert 2 dates to specific format 24 48
sharepoint 2013 "System.Workflow.Activities.StateMachineWorkflowActivity' is obsolete:" 4 21
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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