我正在寻找一种在VBS中存储数据的方法
[ 0 => [posX, posY], 1 => [posX, posY] ]
Run Code Online (Sandbox Code Playgroud)
有点一个关联数组,坐标为值.
Dictionary在VBScript 中调用关联数组.你将坐标存储在它们中,如下所示:
Set coords = CreateObject("Scripting.Dictionary")
coords.Add 1, Array(1, 5)
coords.Add 2, Array(3, 2)
WScript.Echo coords(1)(1) 'ouput: 5
WScript.Echo coords(2)(0) 'ouput: 3
Run Code Online (Sandbox Code Playgroud)
话虽如此,根据您的示例,您可能想要创建一个字典数组而不是数组字典:
Sub AddCoordinates(ByRef list, posX, posY)
Set d = CreateObject("Scripting.Dictionary")
d.Add "posX", posX
d.Add "posY", posX
ReDim Preserve list(UBound(list)+1)
Set list(UBound(list)) = d
End Sub
ReDim coords(-1)
AddCoordinates(coords, 1, 5)
AddCoordinates(coords, 3, 2)
...
WScript.Echo coords(0)("posY") 'ouput: 5
WScript.Echo coords(1)("posX") 'ouput: 3
Run Code Online (Sandbox Code Playgroud)
一组自定义对象将是另一种选择:
Class Point
Private posX_
Private posY_
Public Property Get posX
posX = posX_
End Property
Public Property Let posX(val)
posX_ = val
End Property
Public Property Get posY
posY = posY_
End Property
Public Property Let posY(val)
posY_ = val
End Property
End Class
Sub AddCoordinates(ByRef list, posX, posY)
Set p = New Point
p.posX = posX
p.posY = posX
ReDim Preserve list(UBound(list)+1)
Set list(UBound(list)) = p
End Sub
ReDim coords(-1)
AddCoordinates(coords, 1, 5)
AddCoordinates(coords, 3, 2)
...
WScript.Echo coords(0).posY 'ouput: 5
WScript.Echo coords(1).posX 'ouput: 3
Run Code Online (Sandbox Code Playgroud)