我正在嘗試一些東西。我有這個系統,其中一些腳本在某些事件處理程序中被觸發。這是在Active Directory中創建用戶期間使用的。VBS - 創建對象,並通過函數和子傳遞?
所以,我想我需要的是一個函數,它基本上收集了正在處理的用戶的屬性 - 希望是一個對象。然後返回該對象並在其他函數和子目錄中使用它。所以我可以只使用「myObject.firstname」來達到我想要的屬性,就是這樣。
如何創建一個對象,在其中創建和設置值並返回整個對象?這在VBS中可能嗎?
編輯:什麼是最好的,如果我可以創建一個對象,持有這些屬性,只要能夠訪問它們,只要我需要。
我基本上是這樣的後:
Public Sub Main()
Set userStream = New objUser
msgbox objUser.firstname
msgbox objUser.lastname
msgbox objUser.username
End Sub
編輯:爲了解釋furter,我有這個庫腳本,被稱爲「libGetUserProperties」 它在此我想要的功能和等級。然後,我只是在運行時運行的實際腳本中導入此庫腳本。看起來是這樣的:
libGetUserProperties:
Public Function getobjUser(ByRef Request, ByVal From)
Dim thisUserObject
Set thisUserObject = New objUser
'If type = 0, data comes from AD
If From = 0 Then
thisUserObject.firstname = "some string"
thisUserObject.lastname = "some string"
End If
'If type = 1, data comes from Request stream
If From = 1 Then
thisUserObject.firstname = "some string"
thisUserObject.lastname = "some string"
End If
Set getobjUser = thisUserObject
End Function
Class objUser
Public firstname
Public lastname
Public username
End Class'
這是實際運行腳本的樣子:
Dim libGetUserProperties
Set libGetUserProperties = ScriptLib.Load("Script Modules/Library Scripts/libGetUserProperties")
Sub onPreCreate(Request)
Dim userStream
Set userStream = New objUser
'Do whatever with objUser which contains all of the properties I'm after
End Sub
嗯,事情是。我無法使用Scripting.Dictionary,因爲我在創建用戶之前獲取了這些數據。我確實有使用JScript的選項。和PerlScript一樣。 VBscript就是我用過的東西。 – 2011-03-24 12:16:48
當用戶創建時,Dictionary與用戶有什麼關係?你從哪裏獲得數據? – Martijn 2011-03-24 12:21:45
我從名爲Request的參考中獲取數據。它通過稱爲onPreCreate(Request)的事件處理程序自動完成。請求包含正在創建的對象的數據。編輯:看起來像我誤解了Scripting.Dictionary以及 – 2011-03-24 12:29:47