Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:37:15+00:00 2026-05-27T03:37:15+00:00

I’ve coded up a templated digraph class ( Graph<Generic> ) for use with Djikstra’s

  • 0

I’ve coded up a templated digraph class (Graph<Generic>) for use with Djikstra’s in a project I’m working on. In it, it allocates memory for a DataContainer object that is initialized with an AdjacencyList instance (which implements IDataContainer) for keeping track of all the nodes and edges. I’ve also coded up an AdjacencyMatrix class (which also implements IDataContainer) that I’d like the option of dynamically using in some cases.

Right now I create a digraph with the following call:

Graph<string> graph = new Graph<string>();

And in my graph, I create my data container with:

IDataContainer<Generic> data;
public Graph()
{
    data = new AdjacencyList<Generic>();
}

Ideally, I’d like to pass in the data structure I’d like to use (List vs. Matrix) when I call the constructor, like:

Graph<AdjacencyMatrix, string> graph = new Graph<AdjacencyMatrix, string>();

But I’m not quite sure how to pass in a type that’s been templated. I can template it in such a way to allow a call like:

Graph<AdjacencyList<string>, string> graph = new Graph<AdjacencyList<string>, string>();

But when I go to create the AdjacencyList in the class (where public class Graph<Container, Generic> where Container : new() with the where clause as per http://msdn.microsoft.com/en-us/library/x3y47hd4(v=vs.80).aspx) with:

data = new Container();

I get the error:

Cannot implicitly convert type 'Container' to 'GraphDataContainer<Generic>'. An explicit conversion exists (are you missing a cast?)

I could probably include an implicit typecast that would solve the error, but I think the fact that there’s an error at all when trying to create the instance (when passing in a child class to GraphDataContainer) shows that there’s something else wrong here. Is it my inheritance, or is it something convoluted in the messy constructor call (which, if you can think of a cleaner way to do, would be much appreciated!)?

Is there a way to tell a class which class to use to manage its data when it is initially constructed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:37:16+00:00Added an answer on May 27, 2026 at 3:37 am

    What about

    public class Graph<Container> where Container : DataContainer, new()
    

    Then you can use

    var graph = new Graph<AdjacentList>()
    var anotherGraph = new Graph<AdjacentMatrix>()
    

    and in the Graph class

    this.container = new Container();
    

    This could be also accomplished with interfaces

    For example, consider the Graph class contructor. You could make something like:

    public Graph(IContainer container)
    {
       this.container = container;
    }
    

    and have AdjacentList and AdjacentMatrix implement IContainer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.