Archived

This forum has been archived. Please start a new discussion on GitHub.

C# FileSystem from book

I just wanted to post about a problem I had when trying to implement the simple Filesystem example in the ICE documentation. The method that returned an array of Node proxies from the Directory implementation was generating an exception casting. The following lines of code exhibited the behavior:

public override NodePrx[] list(Ice.Current current)
{
return (NodePrx[])_contents.ToArray(typeof(NodePrx[]));
}

This method is inside of the Directory implementation class DirectoryI.

Upon changing the code to the following I was able to fix the error:

public override NodePrx[] list(Ice.Current current)
{
NodePrx[] arr = new NodePrx[_contents.Count];
_contents.CopyTo(arr);
return arr;
}

Hope this helps anyone else trying to follow the C# examples in the book and maybe the devs can explain if there is a better way to fix it.

Also let me know if this is the right forum to post this kind of stuff.

Comments

  • Re: C# FileSystem from book
    Originally posted by stephenhardeman
    I just wanted to post about a problem I had when trying to implement the simple Filesystem example in the ICE documentation. The method that returned an array of Node proxies from the Directory implementation was generating an exception casting. The following lines of code exhibited the behavior:

    public override NodePrx[] list(Ice.Current current)
    {
    return (NodePrx[])_contents.ToArray(typeof(NodePrx[]));
    }

    Oops, there is a typo in that. It should be:
    return (NodePrx[])_contents.ToArray(typeof(NodePrx));
    

    Note the extraneous [] in the call to typeof in the incorrect code.
    Upon changing the code to the following I was able to fix the error:

    public override NodePrx[] list(Ice.Current current)
    {
    NodePrx[] arr = new NodePrx[_contents.Count];
    _contents.CopyTo(arr);
    return arr;
    }

    Hope this helps anyone else trying to follow the C# examples in the book and maybe the devs can explain if there is a better way to fix it.

    See above. Your version works, but using the method on ArrayList is likely to be more efficient (and is certainly no worse than making an explicit copy).

    Also let me know if this is the right forum to post this kind of stuff.

    Yes, absolutely! Thanks for the bug report. I've fixed this for the next release.

    Cheers,

    Michi.