views:

294

answers:

2

I'm using the NVelocity Templating engine to produce a fixed-length field output - you know the kind of thing:

Field        Start Pos   Field Length  Notes
----------   ---------   ------------  ---------
Supplier      1           7            Leading Zeros
GRN           8           9            -
...

e.g.
>0001234    123A<

The problem is I'm trying to call String.PadRight() with the overload to specify the leading zero, and NVelocity is having none of it..

This works:

$Document.SupplierCode.PadRight(7)

But this doesn't:

$Document.SupplierCode.PadRight(7,"0")

I've tried:

  • Single Quotes ('0')

  • Double Single-Quotes (''0'')

  • Double Quotes ("0")

  • Double Double-Quotes (""0"")

  • Escaping the quotes for all of the above (\"0\")

  • No Quotes!

All I've found to work from is the NVelocity Homepage, and the Velocity Templating Language Reference page, niether are pointing me at a solution.

Sorry I'm unable to supply or point you somewhere where you can test out your ideas for yourself, but any suggestions you may have will be most welcome!

Thanks for your help ;o)

+1  A: 

One solution that a colleague has come up with is to create another property in the Document object that returns the formatted String:

E.g.

Public ReadOnly Property SupplierCodeFormatted() As String
    Get
        Return Supplier.Code.PadLeft(7, "0")
    End Get
End Property
Andrew
ooh! with no other answers (at the time of writing), I get to accept my own :o)
Andrew
+1  A: 

I'm coping with the same problem at the moment, as far as I understand it is due to the fact that PadLeft and PadRight functions of String class receive the second parameter, the leading "0", as a char, not as a string.

NVelocity allows you to specify the parameter as a string using '0', but in this way internally it generate a cast exception (or something similar), because the parameter is expected as char.

I haven't found yet (I'm just using NVelocity since 1 hour!) a way to specify the parameter as char, at the moment I have just a dirty solution such as applying a Replace(" ", "0") after the PadLeft / PadRight, so the template becomes

$Document.SupplierCode.PadRight(7).Replace(' ', '0')

ste8
I like your thinking ;o)It didn't feel right adding Formatted field properties to the objects to get around this task in NVelocity. I think your approach is better
Andrew