简体   繁体   中英

c# unable to cast object of type 'system.int16[*]' to type 'system.Int16[]'

I am reading the values from plc tags

public void synch_read() //reads device
{
    Array values;
    Array errors;
    object qualities = new object(); //opc server will store the quality of the item 
    object timestamps = new object(); //store the timestamp of the read

    //read directly from device
    oGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 2, ref handles, out values, out errors, out qualities, out timestamps);

     String abcd = (Int16[])qualities.ToString();
}

In this line

String abcd = ((Int16[])qualities).ToString();

I am getting the error

unable to cast object of type 'system.int16[*]' to type 'system.Int16[]'

How can I solve this error?

EDIT

I tried

Int16[] abcd = (Int16[2])qualities;

error ; expected ; expected

The system.int16[*] is a multidimensional array, not a single dimensional array.

Array array = (Array)qualities;
int dimensions = array.Rank;

If dimensions is 2, then it is a int[,] . If it is 3 it is int[,,] and so on.

For iterating the array with foreach see for example https://stackoverflow.com/a/2893367/613130

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