473,406 Members | 2,467 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,406 software developers and data experts.

Read next in foreach ( string s in myArray )

Hello,

I have a this:
foreach ( string s in myArray ) {

if ( s == ""test") {
}

}

How can I read the next "s" (if there is a next one) inside the current
readed "s"?

Thanks!
Arjen

Nov 15 '05 #1
5 32279
L#
On Sun, 1 Feb 2004 15:59:58 +0100, "Arjen" <bo*****@hotmail.com>
wrote:
Hello,

I have a this:
foreach ( string s in myArray ) {

if ( s == ""test") {
}

}

How can I read the next "s" (if there is a next one) inside the current
readed "s"?

Thanks!
Arjen


A foreach loop is designed to loop through a collection and only
expose one item in the collection at a time. What you can do, is
change the logic in something like this:

string[] arrItems = new string[5];
arrItems[0] = "test0";
arrItems[1] = "test1";
arrItems[2] = "test2";
arrItems[3] = "test3";
arrItems[4] = "test4";

string strPrevious = null;
foreach (string str in arrItems)
{
if (strPrevious != null)
{
// strPrevious is the previous item
// str is the current item
}
strPrevious = str;
}
--
Ludwig
Nov 15 '05 #2
Thanks for your answer but I need to look forward.
I don't care if it is a while, for each, etc. statement.

But I need to read one item forward.
I also want to check if there is a next item.

Arjen


"L#" <no**@none.com> schreef in bericht
news:i4********************************@4ax.com...
On Sun, 1 Feb 2004 15:59:58 +0100, "Arjen" <bo*****@hotmail.com>
wrote:
Hello,

I have a this:
foreach ( string s in myArray ) {

if ( s == ""test") {
}

}

How can I read the next "s" (if there is a next one) inside the current
readed "s"?

Thanks!
Arjen


A foreach loop is designed to loop through a collection and only
expose one item in the collection at a time. What you can do, is
change the logic in something like this:

string[] arrItems = new string[5];
arrItems[0] = "test0";
arrItems[1] = "test1";
arrItems[2] = "test2";
arrItems[3] = "test3";
arrItems[4] = "test4";

string strPrevious = null;
foreach (string str in arrItems)
{
if (strPrevious != null)
{
// strPrevious is the previous item
// str is the current item
}
strPrevious = str;
}
--
Ludwig

Nov 15 '05 #3
L#
On Sun, 1 Feb 2004 16:23:17 +0100, "Arjen" <bo*****@hotmail.com>
wrote:
Thanks for your answer but I need to look forward.
I don't care if it is a while, for each, etc. statement.

But I need to read one item forward.
I also want to check if there is a next item.

Arjen


In fact, you are looking forward. The second time you loop, you have
the previous item and the current item. So in fact you are looking
forward, because the first item is strPrevious and the next item is
str. So use strPrevious as the current item and str as the next item.
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #4
L#
On Sun, 01 Feb 2004 15:27:14 GMT, L#
<ludwig_(nospamplease)stuyck@pandora(nospamplease) .be> wrote:
On Sun, 1 Feb 2004 16:23:17 +0100, "Arjen" <bo*****@hotmail.com>
wrote:
Thanks for your answer but I need to look forward.
I don't care if it is a while, for each, etc. statement.

But I need to read one item forward.
I also want to check if there is a next item.

Arjen


Addendum:

it may be easier to use a normal for loop:

for (int i=0; i<arrItems.Length; i++)
{
string strCurrentItem = arrItems[i];
if (i < arrItems.Length-1)
{
string strNextItem = arrItems[i+1];
}
}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #5
Yes, the last one I will use.
Thanks!

Arjen
"L#" <ludwig_(nospamplease)stuyck@pandora(nospamplease) .be> schreef in
bericht news:8m********************************@4ax.com...
On Sun, 01 Feb 2004 15:27:14 GMT, L#
<ludwig_(nospamplease)stuyck@pandora(nospamplease) .be> wrote:
On Sun, 1 Feb 2004 16:23:17 +0100, "Arjen" <bo*****@hotmail.com>
wrote:
Thanks for your answer but I need to look forward.
I don't care if it is a while, for each, etc. statement.

But I need to read one item forward.
I also want to check if there is a next item.

Arjen


Addendum:

it may be easier to use a normal for loop:

for (int i=0; i<arrItems.Length; i++)
{
string strCurrentItem = arrItems[i];
if (i < arrItems.Length-1)
{
string strNextItem = arrItems[i+1];
}
}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be

Nov 15 '05 #6

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

Similar topics

2
by: ABC | last post by:
How to read the connection string from web.config? I known .NET framework 2.0 has new features about connection settings, but I don't know how to use. Thanks
4
by: Rich | last post by:
I understand that the stringbuilder object can create a string of text much more quickly/efficiently than a string object. I am just checking - if I have a textfile with say 1000 rows of data -...
5
by: th3dude | last post by:
I've searched quite a bit for this one and i can't find any solid solution for what i'm doing here. Right now i'm geting an xml string from an API, creating an xml file, then read the file with...
3
by: pagoto123 | last post by:
i have 5 textboxes that when i enter a director of a film and press a button it shows me the details of the film in those boxes . now i want to make a button to read next record because a director...
4
by: aarthi28 | last post by:
Hi, I am trying to read every other string from a text file and store it in a vector. I can use istream_iterator to read all strings and store it in a vector. However, I only need the first,...
6
by: Yinghe Chen | last post by:
Hi, Could someone help on how to use python to output the next month string like this? "AUG07", suppose now is July 2007. I think also need to consider Dec 07 case, it is supposed to output...
1
by: deerchao | last post by:
Is there any way I can get all resource keys with in a compiled and embedded resx file? Thanks!
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have an xsd that I want to save as an XML string to store in a DB I can save as a physical file using xsd.WriteXml(@"C:\Temp\Junk\junk.xml"); But I am unable to save to a string so I...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.