Wednesday, October 08, 2008

Fun with Generics.collections

I guess the best way to learn about new language features are to put them in practice, and with that I jumped into using the TDictionary to keep track of some xml nodes I was editing.  It was very easy to implement, but what got me stumped was then how to iterate thru the entire list and apply changes to all of the nodes at once.

Thats when I recalled the new for ... in ... syntax.  Using it was dirt simple.  Since my collection was created as tDictionary all I had to do was create a new variable of tPair and then use the for..in.. syntax to invoke a body of code against every IXmlNode in my tDictionary.


var
aPair : tPair<string,ixmlnode>
begin
for aPair in fDictionary do
aPair.Value.Attributes['dirty'] := '-1';
end;

In my option, this generates code which is much more readable and feels more natural.  Now, on to the next challenge.... finding a place in my project where I can put annoymous methods to practical use.

(EDIT: Updated syntax on variable declaration, as it was incorrect...seems the problem with generics is that they HIDE when copied to HTML.)

2 comments:

Joe White said...

I'm puzzled by your code sample. You're talking about Generics.Collections, but you're not using generics. I'd expect TPair to be generic on the key and value type. Isn't it?

SKamradt said...

You are correct, it should be:

var
  aPair : tPair<string,ixmlnode>;