How can I create object in abstract class without having knowledge of implementation?

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-11T01:10:32Z Indexed on 2010/05/11 1:14 UTC
Read the original article Hit count: 343

Filed under:
|
|
|

Hi,

Is there a way to implement the "CreateNode" method in my library abstract below? Or can this only be done in client code outside the library? I current get the error "Cannot create an instance of the abstract class or interface 'ToplogyLibrary.AbstractNode"

public abstract class AbstractTopology<T>
{
    // Properties
    public Dictionary<T, AbstractNode<T>> Nodes { get; private set; }
    public List<AbstractRelationship<T>> Relationships { get; private set; }

    // Constructors
    protected AbstractTopology()
    {
        Nodes = new Dictionary<T, AbstractNode<T>>();
    }

    // Methods
    public AbstractNode<T> CreateNode()
    {
        var node = new AbstractNode<T>();  // ** Does not work **
        Nodes.Add(node.Key, node);
        }
    }
 }

    public abstract class AbstractNode<T>
    {
        public T Key { get; set; }
    }

    public abstract class AbstractRelationship<T>
    {
        public AbstractNode<T> Parent { get; set; }
        public AbstractNode<T> Child { get; set; }

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET