NVelocity and Collections (and Possibly Arrays)

I just spent 40 minutes banging my head against a pointy sharp wall. I’m using NVelocity as my view engine on a Monorail project and couldn’t figure out how to access an element of a collection.

$collection[0]          // Doesn't work
$collection(0)          // Nuh-uh
$collection.[0]         // Nope
$collection.get(0)      // As per Velocity Docs.  Nope
$collection.get_Item(0) // THIS WORKS!

Ok, so that’s incredibly not obvious. I couldn’t find any reference to it on the Castle Project Website, the Velocity docs the Castle site refers you to, or pretty much anywhere.  I did find it in the google group though, but only after a lot lot lot of searching. So hopefully this will save someone out there a few minutes of their life.

Or maybe I should just switch to Brail.

2 Comments »

  1. Shey Said,

    June 4, 2008 @ 9:08 pm

    $collection.get_Item(0)!!!!!!

    sorta reminds me of C++ STL vectors

  2. Gauthier Segay Said,

    June 15, 2008 @ 1:27 pm

    This is due to :
    - lack of syntax in nvelocity
    - class instance’s indexer being compiled as Item CLR property
    - Instance properties being compiled as get_PropertyName and set_PropertyName CLR methods

Leave a Comment