Fri*_*ave 3 arrays vbscript class nested-class
我的问题可以在下面的示例代码中说明,该代码设置了一个朋友的数据数组,每个朋友都可以有几个电话号码:
Class clsPhoneNo
Dim strType
Dim strNumber
End Class
Class clsPerson
Dim strName
Dim aclsPhoneNo()
End Class
Dim clsFriends()
ReDim clsFriends(3)
Set clsFriend(0) = New Person
clsFriend(0).strName = "Fred"
Set clsFriends(0).aclsPhoneNo(0) = New clsPhoneNo
ReDim clsFriend(0).aclsPhoneNo(2)
Set clsFriend(0).aclsPhoneNo(0).strType = "Home"
Set clsFriend(0).aclsPhoneNo(0) = "01234567890"
Set clsFriend(0).aclsPhoneNo(1).strType = "Work"
Set clsFriend(0).aclsPhoneNo(1) = "09876543210"
Run Code Online (Sandbox Code Playgroud)
但是,VBScript说
Microsoft VBScript compilation error: Expected end of statement
Run Code Online (Sandbox Code Playgroud)
之前 .在第二个ReDim声明中
我需要有aclsPhoneNo元素变量长度,因为我的代码实际上并不是地址簿,但这是一个演示问题的简单示例.
有任何想法吗?
Tom*_*lak 14
数组是解决此问题的错误数据结构.它们是其他语言的首选武器,它们不在VBScript中,因为它们非常缺乏灵活性.
请考虑使用词典.然后放下匈牙利人.
Dim phoneBook: Set phoneBook = New ObjectList
With phoneBook.Append(New Person)
.Name = "fred"
.AddPhoneNo "Home", "01234567890"
.AddPhoneNo "Work", "09876543210"
End with
Dim pers: Set pers = phoneBook.Item(1)
For Each id In pers.PhoneNos.List
Set num = pers.PhoneNos.List(id)
WScript.Echo pers.Name & " #" & id & ": " & _
num.Number & " (" & num.Label & ")"
Next
' ------------------------------------------------------
Class ObjectList
Public List
Sub Class_Initialize()
Set List = CreateObject("Scripting.Dictionary")
End Sub
Sub Class_Terminate()
Set List = Nothing
End Sub
Function Append(Anything)
List.Add CStr(List.Count + 1), Anything
Set Append = Anything
End Function
Function Item(id)
If List.Exists(CStr(id)) Then
Set Item = List(CStr(id))
Else
Set Item = Nothing
End If
End Function
End Class
' ------------------------------------------------------
Class PhoneNo
Public Label
Public Number
End Class
' ------------------------------------------------------
Class Person
Public Name
Public PhoneNos
Sub Class_Initialize()
Set PhoneNos = New ObjectList
End Sub
Function AddPhoneNo(Label, Number)
Set AddPhoneNo = New PhoneNo
With PhoneNos.Append(AddPhoneNo)
.Label = Label
.Number = Number
End With
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
请注意,我在这里使用了一些VB功能
Public和Private类变量Initiate和Terminate你可以做出反应的事件With块也可以为您保存临时变量.| 归档时间: |
|
| 查看次数: |
16719 次 |
| 最近记录: |