gpt4 book ai didi

vbscript - 将坐标存储在关联数组中

转载 作者:行者123 更新时间:2023-12-04 13:39:15 25 4
gpt4 key购买 nike

我正在寻找一种在 VBS 中存储数据的方法,例如

[ 0 => [posX, posY], 1 => [posX, posY] ]

有点像一个以坐标为值的关联数组。

最佳答案

关联数组称为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

话虽如此,鉴于您的示例,您可能希望创建一个字典数组而不是数组字典:

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

自定义对象数组是另一种选择:

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

关于vbscript - 将坐标存储在关联数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39065715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com