带循环的字符串处理 VB.net
String Processing with loop 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:
{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=1057}{
在单个 字符串变量中 。而且该变量只需分配一次(输出实际上出现了四次。我的意思是循环被触发了 4 次)。我不能在单个事件中为这个特定变量多次赋值。这意味着,无论循环为一个事件工作多少次,我都需要将所有输出一次包含在字符串变量中。
现在,我能得到一些帮助吗? :(
[注意:输出数量可能不同。]
您可以将 string.Join 与 linq 表达式一起使用
Dim result = String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList())
(这超出了循环)
您的规范存在的问题是,当您调用循环代码时,您对未来或之前的调用一无所知。然后你需要一个地方来存储中间值并在你完成循环后检索它们
这只能通过 class 全局级别变量来解决。假设您有一个 class 名为 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
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: {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=1057}{
在单个 字符串变量中 。而且该变量只需分配一次(输出实际上出现了四次。我的意思是循环被触发了 4 次)。我不能在单个事件中为这个特定变量多次赋值。这意味着,无论循环为一个事件工作多少次,我都需要将所有输出一次包含在字符串变量中。
现在,我能得到一些帮助吗? :(
[注意:输出数量可能不同。]
您可以将 string.Join 与 linq 表达式一起使用
Dim result = String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList())
(这超出了循环)
您的规范存在的问题是,当您调用循环代码时,您对未来或之前的调用一无所知。然后你需要一个地方来存储中间值并在你完成循环后检索它们
这只能通过 class 全局级别变量来解决。假设您有一个 class 名为 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