题
之前正在查看一些代码,并且我认为必须有更优雅的方式来编写这个....
(returnVar.Warnings是一个字符串数组,可以根据记录的警告数量以任意大小返回)
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
Next
解决方案
使用通用列表(字符串)然后在需要时获取包含列表数据的数组
dim list = new List(of string)
list.Add("foo")
list.Add("bar")
list.ToArray()
其他提示
你不能使用ArrayList为你做这个吗?
http://msdn.microsoft.com/en -us /库/ system.collections.arraylist.aspx
首先将 If
语句移出循环。
如果您使用的是框架3.5,则可以使用LINQ循环项目。
If o.ImageContent.ImageId = 0 Then
returnVar.Warnings = items.Select(Function(item) "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString).ToArray()
Else
returnVar.Warnings = New String() {}
End If
不隶属于 StackOverflow