DLL out int not accepted

I have built a DLL in C#.

In the following function the IDE tells my that the function _api.Version has some invalid arguments. But I don't think that this is true.

    public int getVersion(out int uMaj, out int uMin, out int uBuild, out int uDev)
    {
        ApiError error;
        error = _api.Version(uMaj, uMin, uBuild, uDev); //IDE does not like this
        int iRet = (int)error;
        return iRet;
    }

The "Version" function is defined as follows:

    public ApiError Version(out int major, out int minor, out int build, out int device);

Does anybody see my error? Thank you for the help!

The entire code of my DLL is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using B.Api;

namespace ABWrapperNET
{

    public class clsWrapper
    {
        someApi _api = new someApi();

        public int getVersion(out int uMaj, out int uMin, out int uBuild, out int uDev)
        {
            ApiError error;
            error = _api.Version(uMaj, uMin, uBuild, uDev);
            int iRet = (int)error;
            return iRet;
        }
    }
}
Jon Skeet
people
quotationmark

In C#, you need to specify out and ref modifiers at the call site as well as in the member declaration. That means that anyone reading the code is aware of what is going on. (This is a big boost to readability, in my view.)

So you want:

public int GetVersion(out int uMaj, out int uMin, out int uBuild, out int uDev)
{
    ApiError error = _api.Version(out uMaj, out uMin, out uBuild, out uDev);
    return (int)error;
}

Note:

  • I've fixed the method name to follow .NET naming conventions
  • I've removed the pointless declaration/assignment split for error
  • I've removed the temporary variable for the cast to int; you could reintroduce this for debugging purposes if you want
  • I would personally get rid of the u prefix on each method parameter, having something like:

    public int GetVersion(out int major, out int minor, out int build, out int foo)
    

... replacing foo with whatever the abbreviation "dev" is meant to represent.

You should also consider just returning a Version object, and use exceptions for errors:

public Version GetVersion()
{
    int major, minor, build, foo;
    ApiError error = _api.Version(out major, out minor, out build, out foo);
    if (error != ApiError.Success) // Or whatever it uses
    {
        // You'd probably need to create this class yourself
        throw new ApiException(error);
    }
}

people

See more on this question at Stackoverflow