简体   繁体   中英

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!

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM