473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

foreach operating on 2 string[]

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> );



Nov 15 '05 #1
5 1165
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 [i]
syntax.. no?

--
Girish Bharadwaj

Nov 15 '05 #2
[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
group - at least not on the MS server.]

john bailo <jb****@vestcom.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

you could

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

"Girish Bharadwaj" <girishb@nowhere> wrote in message
news:3F8DBAB8.8050203@nowhere...
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 [i]
syntax.. no?

--
Girish Bharadwaj

Nov 15 '05 #4
Wouldn't it be easier if you use Hashtable in .NET System.Collection?

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> );



Nov 15 '05 #5
john bailo wrote:
you could

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

"Girish Bharadwaj" <girishb@nowhere> wrote in message
news:3F8DBAB8.8050203@nowhere...
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 [i]
syntax.. no?

--
Girish Bharadwaj


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

--
Girish Bharadwaj

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
3
by: Steve Barnett | last post by:
I have a collection which I need to navigate through in several different ways. Is there any way to structure my class such that I can have several different iterators? Ideally, I'd like to do...
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
8
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
An example of a slightly more complicated class might be to have a collection of first names, and a collection of last names in your class. The IEnumerable functions then could return the complete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.