Solved

VS 2013 - Factory Pattern - FileReaderFactory

Posted on 2015-02-22
7
56 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
BTW - Nothing happens if I hit Ctrl + period on iFileReader
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
It's not homework.  I'm just trying to write a factory pattern on my own.
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
@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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now