Addressing an object that is an array – help simplifying my code

Mar 20 2012 9:50 AM

I have a function that is passed an object that may or not be array.  If it is an array, I want to iterate through it.  I was able to write some code that does this, but it involves first creating an explicitly declared array and copying in the contents of the object.  It seems like I should be able to skip this step and iterate through the passed object directly, but I'm not sure how.  Any guidance you could provide would be helpful.

Below is the code segment that works, i.e. where I copy to another array first.

if (arg is object[,])
{
 object[,] myArray = new object[((object[,])arg).GetLength(0), ((object[,])arg).GetLength(1)];
 Array.Copy((object[,])arg, myArray, myArray.Length);
 for (int i = 0; i <= myArray.GetUpperBound(1); i++)
 {
  for (int j = 0; j <= myArray.GetUpperBound(0); j++)
  {
   total = total + dbConnector.lookup(Int32.Parse(myArray[j, i].ToString()));
  }
 }


Answers (4)