Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VS 2013 - Factory Pattern - FileReaderFactory

Posted on 2015-02-22
7
Medium Priority
?
69 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 1500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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