C# equivalent of VB.NET "For Each"

  • Thread starter Thread starter John Grandy
  • Start date Start date
J

John Grandy

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
 
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.
 
Thank you for your very positive statement regarding profesionalism,
camaraderie, the planet, and those who live on it.
 
QWERTY said:

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.
 
Actually there is.

Language Equivalents
http://msdn.microsoft.com/library/d...us/vsintro7/html/vxgrflanguageequivalents.asp

Sam


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.
 
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?
 
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


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?

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.
 
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...
 
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" ?
 
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.
 
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.
 
John Grandy said:
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.
 
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.
 
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.
 
Michael A. Covington said:
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?
 
Back
Top