在 vb.net 中是否有类似 "with" 的功能?
in vb.net is there something similar to "with" but for functions?
我连续用同一个函数,有没有办法像
with basicmove()
{(r),(u, 20),(r)
end with
而不是
basicmove(r)
basicmove(u, 20)
basicmove(r)
编辑:或
basicmove(l, 5)
basicmove(d, 3)
basicmove(l, 21)
basicmove(u, 5)
不,这不可能。但是,如果参数是同一类型,则可以使用基本类型列表和 List.ForEach
或普通 For Each
循环:
moves.ForEach(Function(r) basicmove(r))
您可以创建一个具有适当属性的新 class 和一个 List(Of Move)
。
我不完全确定这是否解决了你所要求的一切,但也许它会给你一个想法。
编辑: 根据更新后的问题,我有另一个解决方案。这就需要在编程中理解two-dimensional arrays。您只需将所有 "coordinates" 加载到一个数组中,然后迭代它们并按照它们输入数组的相同顺序执行操作。
Sub Main()
' multi-dimensional array of moves (there are plenty of way to create this object if this is confusing)
Dim myList(,) As Object = New Object(,) { _
{"l", 5}, _
{"d", 3}, _
{"l", 21}, _
{"u", 5} _
}
' iterate the array and perform the moves.
For x As Integer = 0 To myList.GetUpperBound(0)
' getting coordinates from the first & second dimension
basicmove(myList(x, 0), myList(x, 1))
Next
Console.ReadLine()
End Sub
Private Sub basicmove(ByVal a As String, ByVal b As Integer)
Console.WriteLine("param1:" & a & ",param2:" & b)
End Sub
请原谅 c# 语法。这是未经测试的,因此可能存在错误。
public class myWith{
public List<Object> params{get;set;}
public with(Action<Object> function){
params.ForEach(p=>function(p));
}
}
使用的想法是:
new myWith(x=>doStuff(x)){
params = new List<Object>{r, {u,20}, r}
}
希望这个想法能得到理解
这不是最漂亮的,尽管它可以使用隐式数组:
Private Sub MakeCalls()
Dim r As Integer = 1 : Dim u As Integer = 2
For Each o In {({r}), ({u, 20}), ({r})}
basicmove(o)
Next
End Sub
Private Sub basicmove(params() As Integer)
For Each i As Integer In params
Debug.Print(i.ToString)
Next
End Sub
我连续用同一个函数,有没有办法像
with basicmove()
{(r),(u, 20),(r)
end with
而不是
basicmove(r)
basicmove(u, 20)
basicmove(r)
编辑:或
basicmove(l, 5)
basicmove(d, 3)
basicmove(l, 21)
basicmove(u, 5)
不,这不可能。但是,如果参数是同一类型,则可以使用基本类型列表和 List.ForEach
或普通 For Each
循环:
moves.ForEach(Function(r) basicmove(r))
您可以创建一个具有适当属性的新 class 和一个 List(Of Move)
。
我不完全确定这是否解决了你所要求的一切,但也许它会给你一个想法。
编辑: 根据更新后的问题,我有另一个解决方案。这就需要在编程中理解two-dimensional arrays。您只需将所有 "coordinates" 加载到一个数组中,然后迭代它们并按照它们输入数组的相同顺序执行操作。
Sub Main()
' multi-dimensional array of moves (there are plenty of way to create this object if this is confusing)
Dim myList(,) As Object = New Object(,) { _
{"l", 5}, _
{"d", 3}, _
{"l", 21}, _
{"u", 5} _
}
' iterate the array and perform the moves.
For x As Integer = 0 To myList.GetUpperBound(0)
' getting coordinates from the first & second dimension
basicmove(myList(x, 0), myList(x, 1))
Next
Console.ReadLine()
End Sub
Private Sub basicmove(ByVal a As String, ByVal b As Integer)
Console.WriteLine("param1:" & a & ",param2:" & b)
End Sub
请原谅 c# 语法。这是未经测试的,因此可能存在错误。
public class myWith{
public List<Object> params{get;set;}
public with(Action<Object> function){
params.ForEach(p=>function(p));
}
}
使用的想法是:
new myWith(x=>doStuff(x)){
params = new List<Object>{r, {u,20}, r}
}
希望这个想法能得到理解
这不是最漂亮的,尽管它可以使用隐式数组:
Private Sub MakeCalls()
Dim r As Integer = 1 : Dim u As Integer = 2
For Each o In {({r}), ({u, 20}), ({r})}
basicmove(o)
Next
End Sub
Private Sub basicmove(params() As Integer)
For Each i As Integer In params
Debug.Print(i.ToString)
Next
End Sub