0

I have to write the contents in this format

somename1  value1 value2 value1 value2 ....
somename2  value1 value2 value1 value2 ....
somename3  value1 value2 value1 value2 ....
somename4  value1 value2 value1 value2 ....
somename5  value1 value2 value1 value2 ....

value1 and value2 are the part of the x and y co-ordinates of a point, so for this I have created a class:

class Points
{
 string name;
 double[] X;
 double[] Y;
}

class PointsCollection
{
  List<Points> aPoints
}

//now when accessing the PointCollection
foreach(Points aPoints in PointsCollection)
{
   stream.Write(aPoints.Name)
   //now i have to write the value1 and valud2

}

possible code

//now when accessing the PointCollection
foreach(Points aPoints in PointsCollection)
{
   stream.Write(aPoints.Name)
   int length1 = aPoints.Real;
   int length2 = aPoints.Imag;
   for(int i = 0 ; i < length1; i++)
   {
            stream.Write(aPoints.Real[i]);
            stream.Write(",");
            stream.Write(aPoints.Imag[i]);

    }
     stream.WriteLine();

}

Question : Is it the correct to use for loop inside the foreachloop?

1
  • may be move the internal loop to a different method so that it looks neat. Commented Apr 27, 2012 at 7:59

4 Answers 4

5

Yes, nesting for loops in foreach (or foreach in for) is correct!

Sign up to request clarification or add additional context in comments.

Comments

1

Is it the correct to use for loop inside the foreach loop?

Loops inside loops – "nested loops" – is a normal approach. Languages like C# allow you to freely nest control structures (loops, if, ...) inside each other. Without this many problems would be harder to solve.

The only danger is making functions long and hard to understand and maintain: the solution is to break the inner control structures into their own methods.

Comments

0

Yes it is, except I'd make sure that length1 and length2 are equal before proceeding ;)

You could also do this:

foreach(Points aPoints in PointsCollection) 
{
   stream.Write(aPoints.Name)
   foreach (var complexNumber in aPoints.Real.Zip(aPoints.Imag,
       (real, imag) => new { Real = real, Imag = imag }))
   {
        stream.Write(complexNumber.Real);
        stream.Write(",");
        stream.Write(complexNumber.Imag);

   }
   stream.WriteLine();
}

But that is just showing off and doesn't really make your code clearer. I think your version was nicer and more to the point...

Oh, and try running the code, because your formatting is off whack and you'll need to tinker it a bit first ;)

Comments

0

I will optimize with a StringBuilder and make only one call to stream.Write() for each loop.
Other than that, there is a little error in reading the length of the Real array, but I suppose it is only a typing error here on SO.

StringBuilder sb = new StringBuilder();
//now when accessing the PointCollection 
foreach(Points aPoints in PointsCollection) 
{ 
   sb.Clear();
   sb.Append(aPoints.Name);
   int length1 = aPoints.Real.Lenght;  // Get the length of Real array
   // int length2 = aPoints.Imag; // Not needed???
   for(int i = 0 ; i < length1; i++) 
   { 
        sb.AppendFormat("{0},{1} ", aPoints.Real[i], aPoints.Imag[i]); 
   } 
   sb.AppendLine(); 
   stream.Write(sb.ToString()); 
} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.