Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

VS 2013 - Factory Pattern - FileReaderFactory

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
CipherIS
Asked:
CipherIS
  • 4
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
CipherISAuthor Commented:
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
 
CipherISAuthor Commented:
BTW - Nothing happens if I hit Ctrl + period on iFileReader
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Daniel Van Der WerkenCommented:
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
 
käµfm³d 👽Commented:
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
 
CipherISAuthor Commented:
It's not homework.  I'm just trying to write a factory pattern on my own.
0
 
CipherISAuthor Commented:
@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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now