3

Whats wrong?

    using QuickGraph;
    using GraphSharp;

     public class State
        {
            public string Name { get; set; }
            public override string ToString()
            {
                return Name;
            }
        }

     public class Event
        {
            public string Name;
            public override string ToString()
            {
                return Name;
            }
        }

    BidirectionalGraph<State, TaggedEdge<State, Event>> x =
                    new BidirectionalGraph<State, TaggedEdge<State, Event>>();

    GraphLayout graphLayout = new GraphLayout();
    graphLayout.Graph = x;

Error:

Cannot implicitly convert type QuickGraph.BidirectionalGraph<ChashaGraphSharp.State,QuickGraph.TaggedEdge<ChashaGraphSharp.State,ChashaGraphSharp.Event>> to QuickGraph.IBidirectionalGraph<object,QuickGraph.IEdge<object>>. An explicit conversion exists (are you missing a cast?)

If I put the cast, then application gets fault error on start without any information

What's wrong?

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
Dmitry
  • 31
  • 2

3 Answers3

2

You need to create your instance of BidirectionGraph using the type IEdge instead of TaggedEdge:

BidirectionalGraph<State, IEdge<State, Event>> x =
                new BidirectionalGraph<State, IEdge<State, Event>>();

I can't say that I fully understand why this is the case, however the above should work.

EDIT: I asked a question that kind of explains why this cast doesn't work.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
1

If you use a custom graph (IE not a "BidirectionalGraph<Object, IEdge<Object>") You need to use a custom GraphLayout that inherits from "ContextualGraphLayout"

Here, instead of using "GraphLayout" use "ContextualGraphLayout<State,Edge<<State>>,BidirectionalGraph<State, Edge<State>>>".

I strongly advise to create dummy model classes to gain readability. For instance:

public MyVertex : State { }
public MyEdge : Edge<MyVertex> {
   public MyEdge (MyVertex source, MyVertex target)
      : base(source, target) { }

}

public MyGraph : BidirectionalGraph<MyVertex, MyEdge> { }
public MyGraphLayout : ContextualGraphLayout<MyVertex, MyEdge, MyGraph> {
    public MyGraphLayout () : base() { }

    public MyGraphLayout (bool allowParallelEdges)
        : base(allowParallelEdges) { }

    public MyGraphLayout (bool allowParallelEdges, int vertexCapacity)
        : base(allowParallelEdges, vertexCapacity) { }

}

Dace
  • 43
  • 6
0

Yes

But TaggedEdge does not implement IEdge interface How to use custom TaggedEdge?

Dmitriy Kudinov
  • 1,051
  • 5
  • 23
  • 31