foreach

Visual Studio Snippets - How to specify foreach loop's collection

When using the Visual Studio shortcut/snippets is it possible to specify the collection in advance/automagically, rather than fill in the green boxes afterwards? In this case, I'm trying to come up with something like the following with the fewest possible number of keystrokes: foreach (ListItem item in ListBox1.Items) { // } For...

Java foreach efficiency

I have something like this: Map<String, String> myMap = ...; for(String key : myMap.keySet()) { System.out.println(key); System.out.println(myMap.get(key)); } So is myMap.keySet() called once in the foreach loop? I think it is, but want your opinion. I would like to know if using foreach in this way (myMap.keySet()) has a pe...

jQuery loop though two levels of elements

I have some html that looks like this: <div class="block"> <div id="item1"></div> <div id="item2"></div> </div> <div class="block"> <div id="item3"></div> </div> I'm trying to check to see if my div will contain a specific element. I need to loop though each block, then get each item. What I've got so far $('.block...

Getting position of the XML element inside For Each loop

How do I get the position of the XML element in this loop? For Each objNode In objDoc.SelectNodes("//books/book") ??? Next What I want in output would be something like 1 2 3 4 .... ...

Getting 'invalid argument for foreach' for a valid array in PHP

I'm getting the error Invalid argument supplied for foreach() even though the array being supplied appears to be a valid array. Here's my code: print_r($keywords); extract($product); foreach ($keywords as $k=>$v) { //stuff here } Here's some of the output from the print_r: Array ( [0] => Array ( [text] => v...

how to use foreach in linq

I have read in some blog some time ago (sorry for being vague) that i could use a linq like the following var list = from c in xml select new { foreach(XElement el in c.Elements()) { } } Does anyone know is it possible or is it just my imagination?? Thanks. ...

C# How do you use foreach on a base class using generics?

This question is as a result of trying Jon Skeet's answer to this question. So I have the following code based in the question and answer from the above question link. public abstract class DeliveryStrategy { } public class ParcelDelivery : DeliveryStrategy { } public class ShippingContainer : DeliveryStrategy { } public abstract cla...

ForEach and Facelets

Hello, My managed bean : public List<String> getLiQuickNav(){ System.out.println("I'm here..."); List<String> l = new ArrayList<String>(); l.add("toto"); l.add("tata"); l.add("titi"); return l; } My forEach : <c:forEach var="categorie" items="#{mainControleur.liQuickNav}"> <h:outputLabel value="${categorie}"/> </c:for...

Custom foreach results for dynamic proxy class - magic methods?

I need to serialize a proxy class. The class uses __set and __get to store values in an array. I want the serialization to look like it is just a flat object. In other words, my class looks like: class Proxy { public $data = array(); public function __get($name) { return $data[$name] } } and I want a foreach ...

Foreach can throw an InvalidCastException??? Damn...

Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x = new List<IComparable>(); x.Add(5); foreach (string s in x) { //InvalidCastException in runtime } } } I wonder, why is this foreach behavior so... not C#-like? What ha...

stdClass object and foreach loops

I am using the following code to get data from a website using Soap. $client = new SoapClient('http://some.url.here'); class SMSParam { public $CellNumber; public $AccountKey; public $MessageCount; public $MessageBody; public $Reference; } $parameters = new SMSParam; $parameters -> AccountKey = "$sms_key"; $parameters -> MessageC...

how to modify items in a Generic List using foreach?

I have the following Generic List which is populated with a list of string: List<string> mylist =new List<string>(); myList.add("string1"); myList.add("string2"); Say I want to add 'test' at the end of each string, how can I do it in a simple way? Intuitively, I tried this which compiles ok: myList.ForEach(s => s = s + "test"...

Access Iterator in BOOST_FOREACH loop

I have a BOOST_FOREACH loop to iterate over a list. Unfortunately, I also need to cache an iterator to a particular item. typedef List::iterator savedIterator; BOOST_FOREACH(Item &item, list) { // stuff... if (condition) savedIterator = &item; // this won't work // do more stuff... } Obviously I can do this using a list.b...

How to skip a record in a Foreach

Hello, I am trying to create an object from an Active Directory base on a simple login. The problem is that some of the login information is valid. How could I just use a try-catch so that if an exception is thrown, just skip to the next login? Here is the code: foreach (var PharosUserItem in ListRef) { ADUser User; try {...

automatically get loop index in foreach loop in perl

If I have the following array in Perl: @x = qw(a b c); and I iterate over it with foreach, then $_ will refer to the current element in the array: foreach (@x) { print; } will print: abc Is there a similar way to get the index of the current element, without manually updating a counter? Something such as: foreach (@x) { ...

How can I use the java for each loop with custom classes?

I think most coders have used code like the following : ArrayList<String> myStringList = getStringList(); for(String str : myStringList) { doSomethingWith(str); } How can I take advantage of the for each loop with my own classes? Is there an interface I should be implementing? ...

Deleting items in foreach

Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop? If so, what should be the correct behavior? ...

What is the equivalent of foreach (with keys) in ActionScript

I am looking for the equivalent of a foreach loop with keys in Actionscript. In PHP this would be: foreach($array as $key => $value) { } I found two solutions that will work, but I am wondering if there is a better way to do this. The first solution is to use the for..in loop. Which gives you the keys, but you still have to use the ke...

C# design for an object where some properties are expensive: excuse to make it mutable?

Yes, I know, yet another question about mutable objects. See this for general background and this for the closest analogue to my question. (though it has some C++ specific overtones that don't apply here) Let's assume that the following pseudo code represents the best interface design. That is, it's the clearest expression of the busin...

foreach my $var (@list) -- $var is a reference?

So, I never knew this and I want to get some clarifcation on it. I know if you do foreach (@list){ if you change $_ in that loop it will affect the actual data. But, I did not know that if you did foreach my $var1 (@list){ If you changed $var1 in the loop it would change the actual data. :-/ So, is there a way to loop over @list ...