473,327 Members | 1,979 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,327 software developers and data experts.

C# equivalent of VB.NET "For Each"

What is the C# equivalent to the VB.Net looping structure:

Protected rbl As RadioButtonList

Dim li As ListItem
For Each li In rbl.Items

Next li
Nov 16 '05 #1
17 4690
Believe it or not, it's called "foreach".

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:eF**************@TK2MSFTNGP15.phx.gbl...
What is the C# equivalent to the VB.Net looping structure:

Protected rbl As RadioButtonList

Dim li As ListItem
For Each li In rbl.Items

Next li

Nov 16 '05 #2
C# has foreach as well:

http://msdn.microsoft.com/library/de...us/csref/html/
vclrfTheForeachStatement.asp

Any class that implements IEnumerable will support foreach.

--
Ajay Kalra [MVP - VC++]
aj*******@yahoo.com
"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:eF**************@TK2MSFTNGP15.phx.gbl...
What is the C# equivalent to the VB.Net looping structure:

Protected rbl As RadioButtonList

Dim li As ListItem
For Each li In rbl.Items

Next li

Nov 16 '05 #3
On Mon, 21 Mar 2005 21:38:47 -0500, Ajay Kalra wrote:
Any class that implements IEnumerable will support foreach.


Actually any class with a public GetEnumerator() method that returns an
IEnumerator implementation will support foreach. The class doesn't need to
implement IEnumerable, just have the GetEnumerator() method. There are
times when this subtle difference can be important.
--
Tom Porterfield
Nov 16 '05 #4
RTFM

On Mon, 21 Mar 2005 18:19:38 -0800, "John Grandy"
<johnagrandy-at-yahoo-dot-com> wrote:
What is the C# equivalent to the VB.Net looping structure:

Protected rbl As RadioButtonList

Dim li As ListItem
For Each li In rbl.Items

Next li


Nov 17 '05 #5
Thank you for your very positive statement regarding profesionalism,
camaraderie, the planet, and those who live on it.

"QWERTY" <no@email.com> wrote in message
news:5m********************************@4ax.com...
RTFM

On Mon, 21 Mar 2005 18:19:38 -0800, "John Grandy"
<johnagrandy-at-yahoo-dot-com> wrote:
What is the C# equivalent to the VB.Net looping structure:

Protected rbl As RadioButtonList

Dim li As ListItem
For Each li In rbl.Items

Next li

Nov 17 '05 #6

"QWERTY" <no@email.com> wrote in message
news:5m********************************@4ax.com...
RTFM


He can't look up the statements of one language in the manual of another
language. See the problem? There is no specific "FM" that answers his
question.
Nov 17 '05 #7

Actually there is.

Language Equivalents
http://msdn.microsoft.com/library/de...quivalents.asp

Sam
On Tue, 22 Mar 2005 12:50:59 -0500, "Michael A. Covington"
<lo**@ai.uga.edu.for.address> wrote:

"QWERTY" <no@email.com> wrote in message
news:5m********************************@4ax.com.. .
RTFM


He can't look up the statements of one language in the manual of another
language. See the problem? There is no specific "FM" that answers his
question.


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 17 '05 #8
Yes yes yes I could have looked it up. But what I never understood about
the "RTFM" people is that it takes them longer to type and submit a "RTFM"
answer than it takes them to read the question in the first place, so if
they are so annoyed by their time being wasted by "obvious questions" then
why do they invest the additional time to respond?

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:77********************************@4ax.com...

Actually there is.

Language Equivalents
http://msdn.microsoft.com/library/de...quivalents.asp

Sam
On Tue, 22 Mar 2005 12:50:59 -0500, "Michael A. Covington"
<lo**@ai.uga.edu.for.address> wrote:

"QWERTY" <no@email.com> wrote in message
news:5m********************************@4ax.com. ..
RTFM


He can't look up the statements of one language in the manual of another
language. See the problem? There is no specific "FM" that answers his
question.


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 17 '05 #9

Because once you get a "how do you do for-each in c#" then you get
"how do you do x" and "how do you do y" and on and on and on.

Looking up answers instead of just posting questions is very
beneficial in that you not only get the answer to your question but
almost always end up learning something else unrelated.

Sam
On Tue, 22 Mar 2005 10:32:42 -0800, "John Grandy"
<johnagrandy-at-yahoo-dot-com> wrote:
Yes yes yes I could have looked it up. But what I never understood about
the "RTFM" people is that it takes them longer to type and submit a "RTFM"
answer than it takes them to read the question in the first place, so if
they are so annoyed by their time being wasted by "obvious questions" then
why do they invest the additional time to respond?

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:77********************************@4ax.com.. .

Actually there is.

Language Equivalents
http://msdn.microsoft.com/library/de...quivalents.asp

Sam


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 17 '05 #10
<"John Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Yes yes yes I could have looked it up. But what I never understood about
the "RTFM" people is that it takes them longer to type and submit a "RTFM"
answer than it takes them to read the question in the first place, so if
they are so annoyed by their time being wasted by "obvious questions" then
why do they invest the additional time to respond?


As Samuel has said, it encourages you to look it up next time instead
of asking the question.

Then again, given that you could almost certainly have looked it up in
less time than you had to wait for a response on a newsgroup, I
wouldn't have thought you'd be in the best of positions to criticise
other people's use of time...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
What makes you think that at the time that I posted that I hadn't tried to
look it up ?

What actually happenned is that I was using the wrong type for a member item
of a collection and I misunderstood the resulting error to mean that
"foreach" was incorrect.

At any rate, at this point this entire thread might be a waste of time

IMHO, given the voluminous documentation made available by Microsoft, "RTFM"
could be the response to every question posted here. By that logic, the
very reason for existence of these forums is negated.

And also by that logic, asking the guy sitting in the cube next to you a
question in unadvised. So why don't we all become hermits and go live in
our own separate caves and spend our lives "RTFM" ?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"John Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Yes yes yes I could have looked it up. But what I never understood about
the "RTFM" people is that it takes them longer to type and submit a
"RTFM"
answer than it takes them to read the question in the first place, so if
they are so annoyed by their time being wasted by "obvious questions"
then
why do they invest the additional time to respond?


As Samuel has said, it encourages you to look it up next time instead
of asking the question.

Then again, given that you could almost certainly have looked it up in
less time than you had to wait for a response on a newsgroup, I
wouldn't have thought you'd be in the best of positions to criticise
other people's use of time...

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

Nov 17 '05 #12
<"John Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
What makes you think that at the time that I posted that I hadn't tried to
look it up ?
Because it's so simple to look up, basically. I find it hard to believe
that you could have spent, say, 5 minutes trying to find the answer in
MSDN and on the web without finding it. Heck, just typing the subject
line of your post gives into Google the answer in the very first
result!

So, did you try to look it up, and if so how did you manage to avoid
finding the answer?
What actually happenned is that I was using the wrong type for a member item
of a collection and I misunderstood the resulting error to mean that
"foreach" was incorrect.
Were you using VS.NET? If so, surely the fact that it had syntax
highlighted "foreach" as a keyword would suggest that it was at least
*a* language feature, which would then be very easily to look up in
MSDN.
At any rate, at this point this entire thread might be a waste of time
It will be if you don't take away from it the idea that it's worth
spending a few minutes looking for an answer on the web before asking
on a newsgroup. Newsgroups are a great way of learning things, but for
a lot of things a few minutes of searching gets the answer *much*
faster - and without anyone else having to lift a finger.
IMHO, given the voluminous documentation made available by Microsoft, "RTFM"
could be the response to every question posted here. By that logic, the
very reason for existence of these forums is negated.
Not really. There are plenty of things which are either unclear in the
documentation, or not specifically documented. There are plenty of
questions which aren't easily answered by a Google search or by looking
through MSDN. Those should be the preferred way of finding things out
though, where possible.
And also by that logic, asking the guy sitting in the cube next to you a
question in unadvised. So why don't we all become hermits and go live in
our own separate caves and spend our lives "RTFM" ?


And by your logic, there shouldn't even be MSDN, because it's easier to
get other people to answer your questions than to look for the answer
yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
Strangely, you seem to be more interested in flames than you are in
programming C#. Plenty of misguided threads are turned into productive
threads by using the question as a jumping off point for discussion of
esoteric or controversial details of the topic.

I already explained to you that I did look up "foreach" and I coded it. but
then I received a compile time error I did not understand and, believing
that I had not coded a direct analogy to VB code, I decided to post to the
forum. You are actually reading what I write aren't you ?

In any event, it is my understanding that posters here have neither an
obligation nor a privilege nor a reward to respond to questions, and so I
fail to see why you insist on making such a big deal out of this. There are
thousands and thousands of posts and the vast majority go unread by any
single individual, so what does it matter if someone makes a mistake every
now and then ?

If you personally owned this newsgroup then it might be another matter, but
you don't.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"John Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
What makes you think that at the time that I posted that I hadn't tried
to
look it up ?


Because it's so simple to look up, basically. I find it hard to believe
that you could have spent, say, 5 minutes trying to find the answer in
MSDN and on the web without finding it. Heck, just typing the subject
line of your post gives into Google the answer in the very first
result!

So, did you try to look it up, and if so how did you manage to avoid
finding the answer?
What actually happenned is that I was using the wrong type for a member
item
of a collection and I misunderstood the resulting error to mean that
"foreach" was incorrect.


Were you using VS.NET? If so, surely the fact that it had syntax
highlighted "foreach" as a keyword would suggest that it was at least
*a* language feature, which would then be very easily to look up in
MSDN.
At any rate, at this point this entire thread might be a waste of time


It will be if you don't take away from it the idea that it's worth
spending a few minutes looking for an answer on the web before asking
on a newsgroup. Newsgroups are a great way of learning things, but for
a lot of things a few minutes of searching gets the answer *much*
faster - and without anyone else having to lift a finger.
IMHO, given the voluminous documentation made available by Microsoft,
"RTFM"
could be the response to every question posted here. By that logic, the
very reason for existence of these forums is negated.


Not really. There are plenty of things which are either unclear in the
documentation, or not specifically documented. There are plenty of
questions which aren't easily answered by a Google search or by looking
through MSDN. Those should be the preferred way of finding things out
though, where possible.
And also by that logic, asking the guy sitting in the cube next to you a
question in unadvised. So why don't we all become hermits and go live in
our own separate caves and spend our lives "RTFM" ?


And by your logic, there shouldn't even be MSDN, because it's easier to
get other people to answer your questions than to look for the answer
yourself.

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

Nov 17 '05 #14

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
What makes you think that at the time that I posted that I hadn't tried to
look it up ?


Hear, hear! Although there *is* a language equivalents document, people
aren't born knowing about it, and the normal place to look things up is the
help system. If you only have the help system for one language, it's not at
all obvious whether this information is in it at all.

I might as well answer every question from my students by saying, "Go to the
library." Then I wouldn't be much of a teacher.

There certainly is such a thing as an RTFM question. This didn't strike me
as being one.

Nov 17 '05 #15
> Actually any class with a public GetEnumerator() method that returns an
IEnumerator implementation will support foreach. The class doesn't need to implement IEnumerable, just have the GetEnumerator() method. There are
times when this subtle difference can be important.


Agreed. However I would still be more inclined to support IEnumberable. At
the minimum, it shows the intent. It helps the person reading the code. In
addition, if this foreach is not being used by the client of this class, and
you happen to remove GetEnumerator from the code, nothing will happen at
the compile/run time. However if you have your class implementing
IEnumberable, taking out GetEnumerator will give an error. It preserves the
completeness of design.

My $0.02c.

--
Ajay Kalra [MVP - VC++]
aj*******@yahoo.com
Nov 17 '05 #16
<"John Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Strangely, you seem to be more interested in flames than you are in
programming C#.
Then clearly you're not reading my posts outside this thread.
Plenty of misguided threads are turned into productive
threads by using the question as a jumping off point for discussion of
esoteric or controversial details of the topic.
No doubt - but that doesn't mean that it's a good idea to have those
misguided threads in the first place. Any number of things which are a
bad idea in themselves sometimes lead to good results - that doesn't
mean the original action is any better.
I already explained to you that I did look up "foreach" and I coded it. but
then I received a compile time error I did not understand and, believing
that I had not coded a direct analogy to VB code, I decided to post to the
forum. You are actually reading what I write aren't you ?
Absolutely. However, you didn't explain that you looked it up. You said
that you were using it, but not that you had looked it up. If you
really think that you explained that you'd looked it up, please say
which post you said that in.

Given that if you type in "foreach" in the MSDN index, it gives you a
description of the syntax and lots of examples, it surprises me to hear
that you could have seen those and still asked the question. (And as I
say, a Google search with your subject line gives the information
immediately too.)
In any event, it is my understanding that posters here have neither an
obligation nor a privilege nor a reward to respond to questions, and so I
fail to see why you insist on making such a big deal out of this. There are
thousands and thousands of posts and the vast majority go unread by any
single individual, so what does it matter if someone makes a mistake every
now and then ?
It's a big deal because you don't seem to see a problem wasting
people's time. Even just skipping over a post takes *some* time.

I thought QWERTY's original response was a bit rude, but slightly
deserved. While put a bit strongly, the suggestion that it would have
been better to read the manual (which does make the answer very clear,
IMO) was reasonable.
If you personally owned this newsgroup then it might be another matter, but
you don't.


So does that mean I can't encourage people to ask questions having
looked up documentation? People often ask questions without doing
appropriate research, or without posting nearly enough (or the right)
detail to work out what's gone wrong. The newsgroups would be much more
useful if they did a bit of leg-work first, and I don't see what's
wrong with saying that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #17
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
What makes you think that at the time that I posted that I hadn't tried to
look it up ?
Hear, hear! Although there *is* a language equivalents document, people
aren't born knowing about it, and the normal place to look things up is the
help system. If you only have the help system for one language, it's not at
all obvious whether this information is in it at all.


I suspect John has access to Google though. Try cutting and pasting the
subject line into Google. The very first link gives the answer. The
second link is my C# FAQ which contains a section on "Converting to C#
from VB.NET" which includes a link to the MS "language equivalents"
page
I might as well answer every question from my students by saying, "Go to the
library." Then I wouldn't be much of a teacher.
There's a difference between answering *every* question that way and
answering appropriate questions that way. Being a teacher involves
encouraging people to think for themselves and find answers for
themselves as well as just answering questions directly. ("Teach a man
to fish" etc.)
There certainly is such a thing as an RTFM question. This didn't strike me
as being one.


Despite the answer being very easy to find?

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

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

Similar topics

0
by: Eric Wichterich | last post by:
Hello Pythonistas, does anyone know an equivalent python statement for perl's "next"-statement? (Perl's "next" skips a loop cycle and continues with the next loop cycle - it does not abort the...
8
by: lancevictor | last post by:
I'm learning javascript, and toward that end, I am recreating a page "my way". The original page: http://stenbergcollege.com and the one that I'm creating:...
3
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
3
by: Summit | last post by:
Does anyone know what the C# equivalent for VB6 End is? I'm starting up a form with a boolean test. If I fail, I just want to end the app. Even though I close the form, it picks up on the line...
5
by: TomislaW | last post by:
What is asp.net equivalent for Application("Root") in asp?
11
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType =...
2
by: Tasman | last post by:
Is there an equivalent of valign="bottom" for a table within a DIV using CSS? I've found I can center a table of buttons in a DIV with {margin-left:auto; margin-right:auto}. But I am unsure how...
43
by: Christoph Schneegans | last post by:
Hi! Okay, so positions on "text/html" XHTML are totally contradicting. Anyway! I hope there's more consensus about "application/xml" XHTML. I've recently learned that Opera 9.0b2 does not only...
2
by: Big Bill | last post by:
What's the CSS equivalent for that frequently found table coding? Roughly speaking? I know that speaking of equivalents can itself lead one into a semantic nightmare but, loosely speaking, what...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.