Search code examples
c#dynamicvb6activatorcreateinstance

Calling VB6 method from C# from dynamic object with ByRef and optional Parameters


I usually make calls to class methods dynamically without problems, but only this method not works, always return error:

Dynamic conn = Activator.CreateInstance(Type.GetTypeFromProgID("MyTeam.MyClass"));
bool test = false;
conn.MyFunction(100,"test",DateTime.Now, test, test, "another","another","another");

Original Method of external class with VB:

Public Function MyFunction(ByVal Id As Integer, ByVal Var1 As String, ByVal Fecha As Date, 
                                   Optional ByRef Opcion1 As Boolean = False, 
                                   Optional ByRef Opcion2 As Boolean = False, 
                                   Optional ByVal Var1 As String = "", 
                                   Optional ByVal Var2 As String = "",
                                   Optional ByVal Var3 As String = "" ) As String
  • Question 1: Is it possible to omit optional parameters?

  • Question 2: Is it possible call with ByRef parameters in other way?

UPDATE 1: Yes, C# supports ref/out parameters, but... variables of type "dynamic" calling Method, support this type of input?


Solution

  • In order to pass byref in c# you need to precede the parameter with ref when you call the function too.

    conn.MyFunction(100,"test",DateTime.Now, ref test, ref test, "another","another","another");