Harshit
Harshit

Reputation: 5157

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

Upvotes: 0

Views: 3397

Answers (1)

xanatos
xanatos

Reputation: 111910

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

Upvotes: 2

Related Questions