2012-12-21 3 views
1

의 배열로 푸시하는 객체 만들기 getLocation이 "POINT (xy)"문자열 값을 반환하는 "NL, VENLO, 5928PN"과 같은 여러 주소로 문자열을 분할하는 것입니다. .foreach 내에서 C#

이것은 작동합니다. 다음으로 각 위치에 WayPointDesc 객체를 생성해야합니다. 그리고 이러한 각 객체는 WayPointDesc []에 푸시되어야합니다. 나는 다양한 방법을 시도했지만 지금까지 실현 가능한 옵션을 찾을 수 없습니다. 최후의 수단은 최대의 웨이 포인트를 하드 코딩하는 것입니다.하지만 그런 일은 피하는 편이 낫습니다.

목록을 사용하는 것은 불행히도 옵션이 아닙니다 ... 제 생각에는.

는 기능입니다 : 동적 요소를 추가 할 경우

/* 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? 
    } 
+9

당신이 목록을 사용하고 완료되면 다음에 ToArray를 호출 할 수 없습니다 이유는 무엇입니까? – mletterle

+1

은 wpdStart가 현재 구현 된 방식대로 범위를 벗어나지 않으십니까? –

+0

@mletterle 감사합니다. 나는 방금 그렇게했습니다. – Perfection

답변

5

배열은, 길이를 고정, 당신은 연결리스트 구조의 몇 가지 유형을 사용해야합니다. 또한 원래 추가 할 때 wpdStart 변수가 범위를 벗어났습니다.

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); 
    } 

당신은 정말 나중에 배열로 목록을 원하는 경우 사용 : waypointDesc.ToArray()

+0

ermergurd. 그건 완벽하게 작동합니다. 내가 추가 할 필요가있는 것은 전부였다. 나는 이것을 더 자주 사용할 것이다. xRoute.WaypointDesc [] finalWaypointDesc = waypointDesc.ToArray(); – Perfection

+3

FYI'List '은 (는) 연결된 목록이 아닙니다. 그것은 실제로 후드 아래 동적 배열입니다. – juharr