Search code examples
c#arraysushort

C# Array data transfer to ushort[]


I wrote a method to get data which looks like:

public void DatagridToArray(Array registersArray)
{ 
    registersArray = myModulesList.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray();
}

Now in my usage, I need the data as ushort[], so I declare a variable:

public static ushort[] registers = new ushort[20];

when I call this method in the following way:

this.DatagridToArray(registers);

the Result when I Monitor the DatagridToArray() my registersArray can get a int[7] data, but after the call, the registers variable turn out to be all 0. Please tell me how to do with it, thanks in advance!


Solution

  • You're changing the local reference of registersArray.

    Your code should look like this:

    public ushort[] DatagridToArray()
    { 
        return myModulesList.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray();
    }
    

    And then used like:

    registers = this.DatagridToArray();