I read this tip about the REALbasic language having a convenient "for each" syntax to access the elements of an array.
Here is an example pulled from my code in a REAL Studio 2010 project. My window has an array of threads. I want to kill all of those threads when the window closes.
// Close event handler on my Window
dim t as Thread
for each t in self.usageReporterThreads
if( t <> nil ) then
t.Kill // Kill each thread in the array.
end if
next
By the way, that's an example of polymorphism. The array holds instances of a subclass of Thread. But when I retrieve each instance, I can consider it as "Thread" (the parent class). The method I need ("Kill") is defined as part of "Thread" so there is no need to make this code block aware of the subclass. If I change the subclass, this code continues to work (does not break or need updating).
Here's a generic example of this "for each" syntax:
for each x in someArray
if x <> nil then
// Do some work.
end if
next
No comments:
Post a Comment