foreach operating on 2 string[]

  • Thread starter Thread starter john bailo
  • Start date Start date
J

john bailo

Suppose

string in[], out[]

//both have three elements

foreach ( string str in in[] )
Console.Writeline( str );

writes all the strings in in[]

now i want to loop through both in and out using the same str counter in the
foreach.

how can i do this?

foreach (string str in in[] )
Console.WriteLine( str + " " + <here i want the corresponding out[]
elemement> );
 
john said:
Suppose

string in[], out[]

//both have three elements

foreach ( string str in in[] )
Console.Writeline( str );

writes all the strings in in[]

now i want to loop through both in and out using the same str counter in the
foreach.

how can i do this?

foreach (string str in in[] )
Console.WriteLine( str + " " + <here i want the corresponding out[]
elemement> );
Umm.. Well,.. you can use a *for* loop and take the length and since
both the strings are going to have the same length, just use
syntax.. no?
 
[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
group - at least not on the MS server.]

john bailo said:
string in[], out[]

//both have three elements

foreach ( string str in in[] )
Console.Writeline( str );

writes all the strings in in[]

now i want to loop through both in and out using the same str counter in the
foreach.

how can i do this?

The easiest way of doing it if you were going to do it a lot and really
wanted to keep the foreach syntax would be to create a new class which
took the two arrays and was enumerable, giving out a pair reference for
each iteration - the pair would contain the in and the out, if you see
what I mean.

Unless you're doing it a lot, however, it's probably easier (although
not as pleasant) to just use a normal for loop.
 
you could

but wouldn't it be cooler if you could increment another object
within a foreach ?

Girish Bharadwaj said:
john said:
Suppose

string in[], out[]

//both have three elements

foreach ( string str in in[] )
Console.Writeline( str );

writes all the strings in in[]

now i want to loop through both in and out using the same str counter in the
foreach.

how can i do this?

foreach (string str in in[] )
Console.WriteLine( str + " " + <here i want the corresponding out[]
elemement> );
Umm.. Well,.. you can use a *for* loop and take the length and since
both the strings are going to have the same length, just use
syntax.. no?
 
john said:
you could

but wouldn't it be cooler if you could increment another object
within a foreach ?

john bailo wrote:

Suppose

string in[], out[]

//both have three elements

foreach ( string str in in[] )
Console.Writeline( str );

writes all the strings in in[]

now i want to loop through both in and out using the same str counter in
the
foreach.

how can i do this?

foreach (string str in in[] )
Console.WriteLine( str + " " + <here i want the corresponding out[]
elemement> );

Umm.. Well,.. you can use a *for* loop and take the length and since
both the strings are going to have the same length, just use
syntax.. no?

Well, Maybe you might want to look at Hashtable or some such tuple
handlers. They might allow you to do something similar.
 
Back
Top