Question

I'm trying to create a class structure like this:

public abstract class ParentClass
{
    protected virtual void BuildQueries()
    {
        var Engine = new FileHelperEngine(typeof(TopType));
        DataPoints = Engine.ReadFile(ResumeName) as TopType[];
    }

    protected Parent TopType;
}

public class ChildClass : ParentClass
{
   protected override Child TopType
}

and the types:

public abstract class Parent
{
   //some class members here
}

public class Child : Parent
{
   //some class members here
}

I think there's an easy answer here, but I'm just too new to C# to figure out what I should be googling. I've tried using generics and I just can't get it right.

I know that without the inheritance I'd write

var Engine = new FileHelperEngine(typeof(Parent));

But this is the part of the inheritance that I'm struggling to figure out.

Sorry I failed to mention that FileHelperEngine references the FileHelpers C# library

Was it helpful?

Solution

I do think you're looking for generics, but I'm not totally sure because your question is not clear...

public abstract class ParentClass<T> where T : Parent
{
    protected virtual void BuildQueries()
    {
        var Engine = new FileHelperEngine<T>();
        var r = Engine.ReadFile(ResumeName);
    }

    protected T TopType { get; set; }

    // (...)
}

public class ChildClass : ParentClass<Child>
{
    // don't need to override anything, because your property is generic now
    // which means it will be of type `Child` for this class
}

public class FileHelperEngine<T>
    where T : Parent  // this generic constraint might not be necessary
{
    public T[] ReadFile(string name)
    {
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top