4

I have an interesting question regarding C# code. Basically I have to call a method

BCI2000AutomationLib.IBCI2000Remote.StartupModules(ref System.Array)

Using Visual Studio 2010 the following code compiles and works perfectly:

// Startup modules
string[] modules = new string[3];
modules[0] = "SignalGenerator --local";
modules[1] = "DummySignalProcessing --local";
modules[2] = "DummyApplication --local";
ok_conn = bci.StartupModules(ref modules);

Now porting this to a game engine (e.g. Unity 3D) requires some stricter C# code since it uses Mono C# compiler. So for the same code i get the following compilation error:

The best overloaded method match for 'BCI2000AutomationLib.IBCI2000Remote.StartupModules(ref System.Array)' has some invalid arguments Argument 1: cannot convert from 'ref string[]' to 'ref System.Array'

Can you please give an advice on how to rewrite this code block to a more strict coding to resolve the stated error?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
pjercic
  • 417
  • 1
  • 7
  • 19
  • I can't get the above to compile - you're missing a parameter name on the declaration, and even with that fixed, I get what I expected - an error "cannot convert from 'ref string[]' to 'ref System.Array'". Can you produce a short and complete example that compiles in VS? – Damien_The_Unbeliever Jul 09 '12 at 07:14

2 Answers2

4

Change the type of you variable to System.Array

// Startup modules 
Array modules = new string[3] 
{
    "SignalGenerator --local",
    "DummySignalProcessing --local",
    "DummyApplication --local"
};
ok_conn = bci.StartupModules(ref modules); 

Your method StartupModules takes a ref Array as argument ; it can set the variable to any other Array. Not necessarily a string Array, it could be an int[]. That's why you cannot call with a variable typed as Array of string.

Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • This is perfect. Thanks for the explanation. I was able to compile and run in the Mono C# compiler without problem. Unfortunately Unity3D outputs the error: _ArgumentException: Value does not fall within the expected range. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR (Int32 errorCode) (wrapper cominterop) BCI2000AutomationLib.BCI2000RemoteClass:StartupModules (System.Array&) (wrapper cominterop-invoke) BCI2000AutomationLib.BCI2000RemoteClass:StartupModules (System.Array&)_ But I guess I have to ask the Unity and BCI2000 developers on this error. – pjercic Jul 09 '12 at 09:14
  • Glad to help you, ask them or post a new question here. – Guillaume Jul 09 '12 at 09:59
-1

String Array program, taking string from user:

class Program
{
    static void Main(string[] args)
    {
        int i,j;
        string[] str = new string[10];
        Console.WriteLine("Enter the Name of your friends");
        for (i = 0; i < 10; i++)
        {
            str[i] = Convert.ToString(Console.ReadLine());
            Console.WriteLine("Array["+i+"]="+str[i]);
        }
        Console.ReadLine();
    }
}
koopajah
  • 23,792
  • 9
  • 78
  • 104