List is not being passed as method parameter

Asked

Viewed 171 times

2

I need to implement the method RoutesBetween to trace the route between a point and another (in this case, point To and And).

Test method:

public void TestRoutesBetweenTwoPoints()
{
    var links = new ILink<string>[]
    {
        new Link<string>("a","b"),
        new Link<string>("b","c"),
        new Link<string>("c","b"),
        new Link<string>("b","a"),
        new Link<string>("c","d"),
        new Link<string>("d","e"),
        new Link<string>("d","a"),
        new Link<string>("a","h"),
        new Link<string>("h","g"),
        new Link<string>("g","f"),
        new Link<string>("f","e"),
    };

    var graph = new Graph<string>(links);
    var paths = graph.RoutesBetween("a", "e");

    var list = paths.ToEnumerable().ToArray();
    Assert.AreEqual(list.Length, 2);

    Assert.IsTrue(list.Any(l => String.Join("-", 1) == "a-b-c-d-e"));
    Assert.IsTrue(list.Any(l => String.Join("-", 1) == "a-h-g-f-e"));
}

How will I sweep the list (links) if it is not being passed as method parameter?

The class, constructor and method:

namespace Graph
{
    public interface IGraph<T>
    {
        IObservable<IEnumerable<T>> RoutesBetween(T source, T target);
    }

    public class Graph<T> : IGraph<T>
    {
        public Graph(IEnumerable<ILink<T>> links)
        {

        }
    }

    publi IObservable<IEnumerable<T>> RoutesBetween(T source, T target)
    {
        throw new NotImplementedException();
    }
}
  • @Joãomartins I was making this issue now :P

  • So it was for seconds ;)

  • @When asking a question, put the text of your code, avoid using a print from your editor. This makes it much easier for those who will answer your question and present a [MCVE]

  • @Leandroangelo Leandro, Ok... Thank you

1 answer

2


You can save the "links" reference using the argument passed to the constructor:

public class Graph<T> : IGraph<T>
{
    private IEnumerable<ILink<T>> links;
    public Graph(IEnumerable<ILink<T>> links)
    {
        this.links = links;
    }
}

And then access the link variable in the Routesbetween method.

public IObservable<IEnumerable<T>> RoutesBetween(T source, T target)
{
   // links -> tem valor
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.