Hi,
What does this mean? can you`ll explain me the foreach loop:
foreach (ClassData Columns in Col) where Col is arraylist object
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manoj SevdaPosted Nov 16, 2011, 6:22 AM
using System;
class Program
{
static void Main()
{
// Use a string array to loop over.
string[] ferns =
{
"abcd",
"Efgh",
"ijklmn",
"opqrst"
};
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
}
}
For-loop and foreach-loop
The code that follows illustrates the exact difference between the two loop constructs. You can see that the for-loop has more complexity in its syntax, but this also gives it more power if you want to modify the collection or examine adjacent elements in the array.
using System;
class Program
{
static void Main()
{
string[] rabbits =
{
"abcd",
"Efgh",
"ijklmn",
"opqrst"
};
for (int i = 0; i < rabbits.Length; i++)
{
// Assign string reference based on induction variable.
string value = rabbits[i];
Console.WriteLine(value); // Write the value.
}
foreach (var value in rabbits)
{
Console.WriteLine(value); // Write the value.
}
}
}
Datta KharadPosted Nov 15, 2011, 6:03 AM
You have collection of column names in arraylist and suppose you want pull out column from collection(arraylist) one by one then We use the foreach loop to run through each item, setting the name variable to the item we have reached each time. That way, we have a named variable to output. We declare the name variable to be of the ClassData type - you always need to tell the foreach loop which datatype you are expecting to pull out of the collection.
Ex:- Datatype - ClassData
Object- Columns
Collection- Col
Here you can get each column name from arraylist(Col) one by one.
VulpesPosted Nov 15, 2011, 5:52 AM
So, assuming that's so, the line means take each element in turn from Col, assign it to a local read-only variable called Columns (which only has meaning in the foreach loop) and then do whatever appears in the body of the foreach statement using that variable.
It is equivalent to this for statement:
for(int i = 0; i < Col.Count; i++)
{
ClassData Columns = (ClassData)Col[i];
// do something with Columns
}