Conversion problem object to array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

I use a Custom OPC interface. Aftre reading multiple data I get an object
which includes an array. But in design time this is only declared as object
(OPC definition, can not change this).
I want to assign this object to a DataRow of the same size. The size is
fixed. I tryed to assign this object directly to a datarow, but i received a
narrow conversion error!
I tried a foreach statement but got an compiler error.
During runtime the object I receive is of type array. I tested this with the
following lines

class-definition for better understanding
public class s
{
public object DataValue
public int error
}

int[] newobject=new int[base.Size];

if (s.DataValue.GetType().IsArray) <- TRUE
{
//int j = 0;
//object[] values = ((object[])s.DataValue); <- not allowed
//foreach (object val in s.DataValue)
//{
// newobject[j] = System.Convert.ToInt32(val);
// j++;
//}
}

How can I convert this object to an array of integer ?

Thanx in advance


Wolfgang Baldauf
 
Wolfgang,

If it's actually an int array, you can do

int[] values = (int[])s.DataValue;

If not, this may work better

foreach (object val in (Array)s.DataValue)
....



Mattias
 
Back
Top