How to create a reference to System.Array holding strings in C#
-
19-06-2021 - |
Вопрос
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?
Решение
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.
Другие советы
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();
}
}