473,902 Members | 3,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 32306
L#
On Sun, 1 Feb 2004 15:59:58 +0100, "Arjen" <bo*****@hotmai l.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.co m> schreef in bericht
news:i4******** *************** *********@4ax.c om...
On Sun, 1 Feb 2004 15:59:58 +0100, "Arjen" <bo*****@hotmai l.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*****@hotmai l.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)st uyck@pandora(no spamplease).be
Nov 15 '05 #4
L#
On Sun, 01 Feb 2004 15:27:14 GMT, L#
<ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote:
On Sun, 1 Feb 2004 16:23:17 +0100, "Arjen" <bo*****@hotmai l.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.Leng th; i++)
{
string strCurrentItem = arrItems[i];
if (i < arrItems.Length-1)
{
string strNextItem = arrItems[i+1];
}
}
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #5
Yes, the last one I will use.
Thanks!

Arjen
"L#" <ludwig_(nospam please)stuyck@p andora(nospampl ease).be> schreef in
bericht news:8m******** *************** *********@4ax.c om...
On Sun, 01 Feb 2004 15:27:14 GMT, L#
<ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote:
On Sun, 1 Feb 2004 16:23:17 +0100, "Arjen" <bo*****@hotmai l.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.Leng th; i++)
{
string strCurrentItem = arrItems[i];
if (i < arrItems.Length-1)
{
string strNextItem = arrItems[i+1];
}
}
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

Nov 15 '05 #6

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

Similar topics

2
4135
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
4154
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 - say 50 columns per row uniformly - does it make any difference if I read this data directly into a string from a StreamReader or into a StringBuilder object? Is there any criteria for using a StringBuilder object over a string or vice versa? ...
5
9109
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 XPath or XmlReader, grab the attribute data, dump it into the database. Once all that's done i blow away all the xml files and start the whole process over next time i need to read fresh data from the API.
3
1181
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 may have multiple movies directed . Datagrid doesnt work for me because it not accepts images ............. please help me on this one .
4
3240
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, third,5th etc string in a vector. How do I do this? Thanks a lot. Any help I can get is appreciated
6
11553
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 as below: "JAN07".
1
7363
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
12502
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 can write string to a db I tried to a MemoryStream but it seems to be empty ??? There is data
2
5356
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 db or save string to the db I don't want to have to copy the string to a file to use the WritePrivateProfileString and GetPrivateProfileString. Is there a way around this instead of writing my own class to operate on a string or stream ???
0
11277
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10866
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5892
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6084
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4724
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4305
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.