views:

140

answers:

2

Hi,

I'm trying to use an arraylist as the parameter to String.Format.

            msg = msg & String.Format("<td>{0}</td>" & _
                                      "<td>{1}</td>" & _ 
                                      "<td>{2}</td>" & _ 
                                      "<td>{3}</td>" & _ 
                                      "<td>{4}</td>" & _ 
                                      "<td>{5}</td>" & _ 
                                      "<td>{6}</td>" & _ 
                                      "<td>{7}</td>" & _
                                      "<td>{8}</td>", param)

where param is an ArrayList and the contents are thus (copied from watch list):

+       (0) 9 {Integer} Object
+       (1) 3 {Integer} Object
+       (2) 5 {Integer} Object
+       (3) "180" {String}  Object
+       (4) 0D {Decimal}    Object
+       (5) 6.788D {Decimal}    Object
+       (6) #3/13/2009# {Date}  Object
+       (7) "2004" {String} Object
+       (8) "" {String} Object

But this code throws a FormatException

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Am I wrong that it's possible to use an arraylist? If it is possible, any clues as to why it would be throwing such an error?

Thanks

+3  A: 

Does it accept an ArrayList?

Did you try:

 "<td>{8}</td>", param.ToArray())
Glennular
http://msdn.microsoft.com/en-us/library/b1csw23d(v=VS.90).aspx it accepts System.Object. The reference is basically useless as far as examples, etc.That worked perfectly though, thanks!
Wayne Werner
+1  A: 

You probably need to pass in an object array and not an ArrayList. If you change the code as such you may see what is going wrong:

 msg = msg & String.Format("<td>{0}</td>", param)

It should print something like

< td>System.ArrayList< td>

Have you tried this ?

 msg = msg & String.Format("<td>{0}</td>" & _
                           "<td>{1}</td>" & _ 
                           "<td>{2}</td>" & _ 
                           "<td>{3}</td>" & _ 
                           "<td>{4}</td>" & _ 
                           "<td>{5}</td>" & _ 
                           "<td>{6}</td>" & _ 
                           "<td>{7}</td>" & _
                           "<td>{8}</td>", param.ToArray())
Cobusve