how can I convert the system array to integer array?  
Author Message
Tammt





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

Hello,

How can I convert the system array to integer array

Thanks,

Tammy



Visual C#2  
 
 
louthy





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

array.CopyTo(intArray,0);

 
 
Tammt





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

I'm sorry, my system array is two dimensional array (array aData), its dimention of (1024,1024).

I would like to copy that into pData = new int[1024, 1024];

how can I do that

Thanks,

Tammy


 
 
Paul Louth





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

  public int[,] CopyArrayToIntegerArray(Array src)
  {
   int[,] dest = new int[1024,1024];

   for (int y = 0; y < 1024; y++)
   {
    for (int x = 0; x < 1024; x++)
    {
     dest[x, y] = Convert.ToInt32(src.GetValue(x, y));
    }
   }

   return dest;
  }

You could also generalise it for any size array and type:

  public T[,] Copy2dArray<T>(Array src)
  {
   int w = src.GetUpperBound(0);
   int h = src.GetUpperBound(1);

   T[,] dest = new T[w, h];

   for (int y = 0; y < h; y++)
   {
    for (int x = 0; x < w; x++)
    {
     dest[x, y] = (T)src.GetValue(x, y);
    }
   }
   return dest;
  }

 

I haven't tested this, so please let me know if you have any problems.


 
 
Tammt





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

Thanks Paul, it works!

Tammy


 
 
Paul Louth





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

No problem, glad to be of assistance. The method I put above isn't terribly efficient. So if your code needs to be super quick then give me some more details of what you're trying to do with it and I can maybe make some suggestions for more efficient ways to copy arrays.
 
 
Tammt





PostPosted: Visual C# General, how can I convert the system array to integer array? Top

Thanks for your help.

Yes, I want to copy an array as fast as it can.

Here what I have:

public override int GetImage(out int [,] pData

{

pData = new int[iSize, iSize];

myOptoHardware.axHardwareControl1.OMX_GetImage(ref aData, OMXHardware.IMAGETYPE_OMX.REFLECTED_LIGHT_OMX);

// loop through the 2-D System.Array and populate the 2-D int Array

for (int y = 0; y < aData.GetUpperBound(0); y++)

for (int x = 0; x < aData.GetUpperBound(1); x++)

pData[x, y] = Convert.ToInt32(aData.GetValue(x, y));

}

and somewhere else I would declare call:

int[,] ArrayData;

retval = GetImage(out ArrayData);

Thanks,

Tammy