Domanda

What I am trying to achieve is to split a string into multiple adresses like "NL,VENLO,5928PN" which getLocation will return a "POINT( x y)" string value.

This works. Next I need to create a WayPointDesc object for each location. And each of these objects has to be pushed into the WayPointDesc[]. I have tried various methods but I cannot find a feasable option so far. My last resort is to hardcode a maximum amount of waypoints but I would rather avoid such a thing.

Using a list is unfortunately not an option... I think.

This is the function:

    /* tour()
     * Input: string route
     * Output: string[] [0] DISTANCE [1] TIME [2] MAP
     * Edited 21/12/12 - Davide Nguyen
     */
    public string[] tour(string route)
    {
        // EXAMPLE INPUT FROM QUERY
        route = "NL,HELMOND,5709EM+NL,BREDA,8249EN+NL,VENLO,5928PN"; 
        string[] waypoints = route.Split('+');

        // Do something completly incomprehensible
        foreach (string point in waypoints)
        {
            xRoute.WaypointDesc wpdStart = new xRoute.WaypointDesc();
            wpdStart.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
            wpdStart.wrappedCoords[0].wkt = getLocation(point);
        }

        // Put the strange result in here somehow
        xRoute.WaypointDesc[] waypointDesc = new xRoute.WaypointDesc[] { wpdStart };

        // Calculate the route information
        xRoute.Route route = calculateRoute(waypointDesc);
        // Generate the map, travel distance and travel time using the route information
        string[] result = createMap(route);
        // Return the result
        return result;

        //WEEKEND?
    }
È stato utile?

Soluzione

Arrays are fixed-length, if you want to dynamically add elements, you need to use some type of linked list structure. Also, your wpdStart variable was out of scope when you were adding it originally.

    List<xRoute.WaypointDesc> waypointDesc = new List<xRoute.WaypointDesc>();

    // Do something completly incomprehensible
    foreach (string point in waypoints)
    {
        xRoute.WaypointDesc wpdStart = new xRoute.WaypointDesc();
        wpdStart.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
        wpdStart.wrappedCoords[0].wkt = getLocation(point);

        // Put the strange result in here somehow
        waypointDesc.add(wpdStart);
    }

If you really want the list as an array later, use: waypointDesc.ToArray()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top