使用循环VB.NET处理字符串处理


While i < n
            array(i) = New System.Drawing.Point(points(i).X, points(i).Y)
            res = (array(i).ToString)
            Debug.Print("Res: " & res)
            i += 1
        End While

这是我拥有的代码...

我的输出是 -

res:{x = 1209,y = 67}
res:{x = 1224,y = 66}
res:{x = 1225,y = 82}
res:{x = 1209,y = 82}
res:{x = 40,y = 83}
res:{x = 41,y = 68}
res: {x = 56,y = 68}
res:{x = 54,y = 84}
res:{x = 40,y = 1054}
res:res:res: {x = 41,y = 1040}
res:{x = 56,y = 1040}
res:{x = 55,y = 1056}
res: {x = 1208,y = 1057}
res:{x = 1209,y = 1042}
res:{x = 1224,y = 1042}
res:{x = 1224,y = 1057}

,但我想要这样的 -

} {x = 1209,y = 67} {x = 1224,y = 66} {x = 1225,y = 82} {x = 1209,y = 82} {x = 40,y = 83} {x = 41,y = 68} {x = 56,y = 68} {x = 54,y = 84} {x = 40,y = 1054} {x = 41,y = 1040} {x = 56,y= 1040} {x = 55,y = 1056} {x = 1208,y = 1057} {x = 1209,y = 1042} {x = 1224,y = 1042} {x = 1224,y = 1224,y = 1057} {<1057} {

在单个字符串变量中。而且该变量必须仅分配一次(输出实际上是四次。我的意思是循环触发了4次)。在一个事件中,我不能为此特定变量分配多次值。这意味着,无论循环为事件工作多少次,我都需要一次包含在字符串变量中的所有输出。

现在,我可以得到一些帮助吗?:(

[nb:输出数可能会有所不同。]

您可以使用string.join与linq Expression

一起使用
Dim result = String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList())

(这是在循环之外)

规范的问题是,当您调用循环代码时,您对未来或以前的电话一无所知。然后,您需要一个存储中间值并在循环完成后检索的地方

这只能通过类全球级别变量解决。假设您有一个名为PointProcessor

的类
 Public Class PointProcessor
    ' This will store the intermediate results and 
    ' give back them through the property
    Dim _processingData = new List<string>()

    Public Sub ProcessData(n As Integer)
        Dim i As Integer = 0           
        While i < n
           ' The variables _array_ and _points_ are assumed to be also
           ' global class level variables set by your code sometime before
           ' calling this sub.
            array(i) = New System.Drawing.Point(points(i).X, points(i).Y)
            i += 1
        End While
        ' Store the result of this loop in the global class variable
        _processingData.Add(String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList()))
    End Sub
    ' Give back all the result creating a single string
    Public Property ProcessingResult as String
         Get
             return string.Join(" ", _processingData)
         End Get
    End Property
 End Class

最新更新