1

So I've been struggling on this project for almost a week now and I need help. See I'm trying to have my main program read from three files: MyFileReader, MyCSVReader and MyMorgReader.

On my main program I was trying to create a new simulator along with a new MorgReader that can read from a text file and translate that text file into a simulator based on the text file.

This is what I wrote in the main file of my program:

Program.cs:

class Program
    {
        static void Main(string[] args)
        {
           ...

           ...  

            Simulator sim = new Simulator(Morg1, Morg2, Morg3, Morg4);
            MorgReader mr = new MorgReader(new MyCSVReader(new FileReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt")));
            Morg m;
            while (m = mr.ReadMorg() != null)
                {
                sim.Add(m);
                }

                sim.run();

But three things are not working: the line beginning with MorgReader mr, the while loop, and the Add function within the loop. The first problem generates a CS0144 code which is "Cannot create an instance of the abstract class or interface 'MorgReader', and I'm guess that the last two problems are because of the first one, I'm not sure.

My MorgReader is an abstract class and not an interface. But I have attempted to create another class based on my MorgReader abstract class and then generate a new MorgReader from there. But more errors were generated in my attempt.

I work with four files: Program, MyFileReader.cs, MyCSVReader.cs, and MyMorgReader.cs. Maybe the source of the problem could be in anyone of these files I'm not sure. Don't worry, they're not too much, except for Program.cs, which I'm only posting a portion of it for the sake of time.

MyFileReader.cs:

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

namespace homework3
{
                                                //*FILE READER CLASS*//
    class FileReader : MorgReader
    {

    //Member Variables
        private StreamReader streamReader;

    //FileReader function
        public FileReader(string fileName)
        {
            streamReader = System.IO.File.OpenText(fileName);
            if (streamReader == null)
                throw new Exception("OpenRead() failed for file " + fileName);
        }

    //Read function
        public override string Read()
        {
            System.IO.StreamReader myFile = new System.IO.StreamReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt");
            string myString = myFile.ReadLine();
            return streamReader.ReadLine();
        }

    //Close function
        public override void Close()
        {
            streamReader.Close();
        }
    }
                                            //*end of MyFileReader class*//
}

MyCSVReader.cs:

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

namespace homework3
{
                                                //*MY CSV READER CLASS*//
    class MyCSVReader: ReaderDecorator
    {

    //Member Variables
        string[] buf;
        char[] delimiterSplit = { ',' };
        char[] delimiterAbsorbs = { ' ' };
        int index;

    //My Csv Reader base
        public MyCSVReader(MorgReader wrapped) : base(wrapped)
        {
        }

    //Read Function
        protected override string ReadImpl()
        {
            if (buf == null)
            {
                String line = Wrapped.Read();
                buf = line.Split(delimiterSplit);
                index = 0;
            }

            return buf[index++];
        }

    //Close Function
        public override void Close()
        {
            WrappedReader.Close();
        }

    }
                                                //*end of CSV class*//
}

MyMorgReader.cs:

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

namespace homework3
{
                                            //*MY MORG READER CLASS*//
    abstract class MorgReader
    {
    //Member Variables
        public abstract string Read();
        abstract public void Close();

    }
                                          //*end of morg reader class*//



                                         //*MY READER DECORATOR CLASS*//
    abstract class ReaderDecorator : MorgReader
    {

    //Member Variables
        protected abstract string ReadImpl();
        private MorgReader WrapDecorator;
        protected ReaderDecorator(MorgReader wrapped)
        {
            WrapDecorator = wrapped;
        }

    //WrappedReader function
        protected MorgReader WrappedReader
        { get { return WrapDecorator; } }

    //Read function
        public override string Read()
        {
            var wrapped = WrappedReader.Read();
            if (!string.IsNullOrEmpty(wrapped))
            { wrapped += ","; }

            return wrapped + ReadImpl();
        }

    //Wrapped function
        protected MorgReader Wrapped
        {
            get { return WrapDecorator; }
        }
    }

                                //*end of My Reader Decorator Class*//
}

Please let me know if you need more info on my problem. I'll be happy to oblige. I just want to get rid of this error once and for all.

Thank you in advance.

miiworld2
  • 296
  • 1
  • 2
  • 11
  • 2
    Why is `MorgReader` an abstract class? If it makes sense to make it abstract, why are you trying to instantiate it? Also, please try to only include relevant parts of the code (In this case, `MorgReader` (which you didn't include)) and only the line causing the error `MorgReader mr = new MorgReader(new MyCSVReader(new FileReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt")));`) – Rob Nov 04 '16 at 00:29
  • `MorgReader` is abstract, i.e. it has methods that are just a signature without an implementation. If you call `mr.Read()` you are invoking a method with no implementation, that's why the compiler raises an error when you try to create an instance of the abstract class. Why are you trying to create a `MorgReader` instead of a `MyCSVReader` directly? – KMoussa Nov 04 '16 at 00:44
  • 1
    also see http://stackoverflow.com/questions/16342494/why-abstract-class-cannot-be-instantiated-what-is-the-use-of-a-class-which-cann , http://stackoverflow.com/questions/2308786/abstract-classes-and-interfaces-in-c-sharp , http://stackoverflow.com/questions/6611412/cannot-create-an-instance-of-the-abstract-class-or-interface – Fiddles Nov 04 '16 at 01:17

2 Answers2

1

To understand why this is not working you need to better understand what the compiler it trying to tell you with the error message.

Consider this line of your code found in the Program.cs file:

MorgReader mr = new MorgReader(new MyCSVReader(new FileReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt")));

Now I assume that line is producing this compiler error message:

CS0144 code which is 
    "Cannot create an instance of the abstract class or interface 'MorgReader'"

If you then look at the definition of the MorgReader class you can see it is defined as abstract:

abstract class MorgReader
{
//Member Variables
    public abstract string Read();
    public abstract void Close();  // I cleaned this line for you
}

Now lets assume the compiler was wrong and it accepted this code as valid:

MorgReader mr = new MorgReader(new MyCSVReader(new FileReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt")));

mr.Read();
mr.Close();

In the code above where would the code for Read and Close methods be defined?

In other words what code would be run when you called those methods?

The answer is the code for those methods is not defined and hence the reason the compiler does not allow you to construct an abstract class.

Edit: But that line also looks wrong for other reasons as you don't define a constructor for the MorgReader that takes a MyCSVReader object.

jussij
  • 10,370
  • 1
  • 33
  • 49
0

Please remove the abstract keyword from ReaderDecorator class and use it with a main programme to initiate readers , because you can not initiate abstract class with a new keyword.

MorgReader mr = new ReaderDecorator (new MyCSVReader(new FileReader("C:\\Users\\miiwo\\Dropbox\\Morg.txt")));