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;
}
}
}
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:
error
int
; you could reintroduce this for debugging purposes if you wantI 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);
}
}
See more on this question at Stackoverflow