0

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

Harshit
  • 5,147
  • 9
  • 46
  • 93

1 Answers1

2

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

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280