Search code examples
c#apixserver

Convert identical objects from different namespaces?


These are the errors:

Error   1   Cannot implicitly convert type 'Plantool.xRoute.Point' to 'Plantool.xMap.Point' 
Error   2   Cannot implicitly convert type 'Plantool.xRoute.Point' to 'Plantool.xMap.Point' 
Error   3   Cannot implicitly convert type 'Plantool.xRoute.LineString' to 'Plantool.xMap.LineString'

I have this code which comes with a namespace.

using Plantool; //Contains xMap, xServer, xLocate

And this is the function in question.

    /* createMap()
     * Input: WaypointDesc[], Route
     * Output: string mapURL
     * Edited 21/12/12 - Davide Nguyen
     */
    private static string createMap(xRoute.WaypointDesc[] waypointDesc, xRoute.Route route)
    {
        #region boundingBox
        // Set boundingBox fand use corners from the calculated route
        xMap.BoundingBox boundingBox = new xMap.BoundingBox();
        boundingBox.leftTop = route.totalRectangle.rightTop;
        boundingBox.rightBottom = route.totalRectangle.leftBottom;
        #endregion

        #region mapParams
        // Build mapParams
        xMap.MapParams mapParams = new xMap.MapParams();
        mapParams.showScale = true;
        mapParams.useMiles = false;
        #endregion

        #region imageInfo
        // Create imageInfo and set the frame size and image format. NOTE: 1052; 863
        xMap.ImageInfo imageInfo = new xMap.ImageInfo();
        imageInfo.format = xMap.ImageFileFormat.PNG;
        imageInfo.height = 1052;
        imageInfo.width = 863;
        imageInfo.imageParameter = "";
        #endregion

        #region layers
        // Create a line from the calculated route
        xMap.LineString[] lineStrings = new xMap.LineString[] { route.polygon };
        xMap.Lines[] lines = new xMap.Lines[1];
        xMap.LineOptions options = new xMap.LineOptions();
        xMap.LinePartOptions partoptions = new xMap.LinePartOptions();
        partoptions.color = new xMap.Color();
        partoptions.visible = true;
        partoptions.width = -10;
        options.mainLine = partoptions;

        lines[0] = new xMap.Lines();
        lines[0].wrappedLines = lineStrings;
        lines[0].options = options;

        // Define customLayer that contains the object lines and set layers.
        xMap.CustomLayer customLayer = new xMap.CustomLayer();
        customLayer.visible = true;
        customLayer.drawPriority = 100;
        customLayer.wrappedLines = lines;
        customLayer.objectInfos = xMap.ObjectInfoType.NONE;
        customLayer.centerObjects = true;
        xMap.Layer[] layers = new xMap.Layer[] { customLayer };
        #endregion

        #region includeImageInResponse
        // Set argument includeImageInResponse to false (default).
        Boolean includeImageInResponse = false;
        #endregion

        // Return object map using the following method.
        xMap.Map map = xMapClient.renderMapBoundingBox(boundingBox, mapParams, imageInfo, layers, includeImageInResponse, null);

        // Retrieve the image
        string result = "http://" + map.image.url;

        // Return the drawn map
        return result;
    }

The problem lies with the boundingBox object and the lineString object. route.totalRectangle contains a Point object from the xRoute namespace which is identical to that of xMap. Is there anyway to copy or convert it?

This issue does not seem to happen in java examples, but it does in C#. I am sure that if I can solve this error, the other ones will be solved aswell. I have searched my ass off on the API, but it may help you:

Still digging myself.


Solution

  • In C# you cannot convert from one type to another, even if they are for all purposes identical, without copying all the properites, etc. unless an implicit conversion exists.

    So you can either write a implicit conversion opertor as shown in link above or you could use a tool like AutoMapper to copy between the two objects