Looping thru Arrays

  • Thread starter Thread starter Krish
  • Start date Start date
K

Krish

I have Type System.Object and the value is {System.Array}
I see [0].......[100] in the watch window.

How do I traverse such a data structure.

thanks for any help.
 
I have Type System.Object and the value is {System.Array}
I see [0].......[100] in the watch window.

How do I traverse such a data structure.

thanks for any help.

You'll have to know the type of the base element. If it is object:

foreach(object obj in myArray)
{
// use obj
}

An alternative, using random access (indexing):

for (int i = 0; i < myArray.Length; i++)
{
// use myArray
}

--
Rudy Velthuis

"If the automobile had followed the same development cycle as the
computer, a Rolls-Royce would today cost $100, get a million miles
per gallon, and explode once a year, killing everyone inside."
-- Robert X. Cringely.
 
...
I have Type System.Object and the
value is {System.Array}
I see [0].......[100] in the watch window.

How do I traverse such a data structure.

Say that the variable name of your object is o, you can simply do:

Array a = (Array) o;
foreach (object x in a)
{
// Do something with x
}

You would probably want to know what type the array is containing as well,
e.g.:

Type t = ((Array)o).GetValue(0).GetType();

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top