Call C# dll from VB

2019-06-07 06:01发布

I am using a C# DLL from an VB project, so far I hadn't problems, but after updating DLL version I have some compilation errors when calling functions, I'm not sure if the problem comes from optional parameters or output parameters.

In short, my problem is the opposite to this one.

This is an example of function definition in DLL (if I fix this one it happens in other function calls, it's a BIG dll):

public static bool RequestCode(string countryCode, string phoneNumber, out string password, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")
public static bool RequestCode(string countryCode, string phoneNumber, out string password, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")
public static bool RequestCode(string countryCode, string phoneNumber, out string password, out string request, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")

This is my call from VB (all of them throw error):

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, cc, method)

Or

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, method)

Or

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, method, Nothing, Nothing, Nothing, "204", "")

Or

result = ThatLibrary.ThatClass.RequestCode(countryCode:=pais, phoneNumber:=telefono, password:=password, method:=metodo, id:=Nothing, language:=Nothing, locale:=Nothing, mcc:="204", salt:="")

And this is error message:

Error 3 'RequestCode' is ambiguous because multiple kinds of members with this name exist in class 'ThatClass'.

After some days looking for a soluction I'm considering moving all my project to C# but this is a huge task so I hope there's a simple solution I missed...

标签: c# .net vb.net dll
1条回答
走好不送
2楼-- · 2019-06-07 06:10

You have to name all your parameters for this case to work in VB. Your last method call (naming all parameters) works with the following test:

C# dll:

namespace ClassLibrary1
{
    class ThatClass
    {
        public static string RequestCode(string countryCode, string phoneNumber, out string password, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = ""; return ""; }
        public static string RequestCode(string countryCode, string phoneNumber, out string password, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = "";  response = ""; return ""; }
        public static string RequestCode(string countryCode, string phoneNumber, out string password, out string request, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = ""; response = ""; request = ""; return ""; }
    }
}

VB project, referencing the C# dll:

Class ThisClass
    Sub testing()
        Dim country As String = "USA"
        Dim telephone As String = "555-555-5555"
        Dim pass As String = ""
        Dim cc As String = ""
        Dim method As String = ""
        Dim test As String = ClassLibrary1.ThatClass.RequestCode(countryCode:=country, phoneNumber:=telephone, password:=pass, method:=method, id:=Nothing, language:=Nothing, locale:=Nothing, mcc:="204", salt:="")
    End Sub
End Class
查看更多
登录 后发表回答