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.