Take the 2-minute tour ×

Was looking at some code earlier, and am thinking that there has to be a more elegant way of writing this....

(returnVar.Warnings is a string array, it could be returned as any size depending on the number of warnings that are logged)

For Each item In items
  If o.ImageContent.ImageId = 0 Then
    ReDim Preserve returnVar.Warnings(returnVar.Warnings.GetUpperBound(0) + 1)
    returnVar.Warnings(returnVar.Warnings.GetUpperBound(0)) = "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString
  End If
share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

use the generic List(of string) then get an array containing the list data if you need it

dim list = new List(of string)
share|improve this answer
Note: The ToArray method doesn't return the underlying array, it creates a new array and copies the values to it. –  Guffa Mar 6 '09 at 17:35
good point, post updated –  Jason Mar 7 '09 at 2:48
add comment

Can't you use ArrayList which does this for you?


share|improve this answer
arraylist is something that should go away, it's not typesafe, use List(of T) –  Fredou Mar 6 '09 at 17:22
add comment

Start by moving the If statement out of the loop.

If you are using framework 3.5, you can use LINQ to loop the items.

If o.ImageContent.ImageId = 0 Then
	returnVar.Warnings = items.Select(Function(item) "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString).ToArray()
	returnVar.Warnings = New String() {}
End If
share|improve this answer
add comment

Your Answer


