2011-03-24 88 views
0

我正在嘗試一些東西。我有這個系統,其中一些腳本在某些事件處理程序中被觸發。這是在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 

回答

2

使用VBScript,除非你想動態生成的代碼這是不可能的類,包括屬性,將其保存到文件中,然後將該文件作爲腳本執行。

使用JScript這非常容易。由於Windows Scripting可以處理JScript以及VBScript,所以我建議採用這種方式。

如果您確實需要VBScript中,你可以去最好的是,將包含屬性鍵一的Scripting.Dictionary:

Dim dicUser 
dicUser = CreateObject("Scripting.Dictionary") 
dicUser("FirstName") = objUser.FirstName 

這將允許您按如下方式訪問的第一個名字:

dicUser("FirstName") 

更新

如果你想寫它創建了一個字典,並返回一個函數,這是如何做到這一點在VBScript:

Function RememberTheUser(Request) 
    Dim dicResult 
    Set dicResult = CreateObject("Scripting.Dictionary") 
    dicResult("FirstName") = Request.GetTheFirstName 
    Set RememberTheUser = dicResult 
End Function 

然後可以分配給其他變量:

Dim dicUser ' in the main section of the script, this means it's a global variable 

Sub onPreCreate(Request) 
    ' ... 
    Set dicUser = RememberTheUser(Request) 
    ' ... 
End Sub 


Sub onSomethingLater() 
    WScript.Echo dicUser("FirstName") ' It's still accessible here 
End Sub 

在JScript中,它更容易:只需創建一個新對象,並添加新屬性。

function readObject(dataSource) { 
    var result = {}; 
    result.source = dataSource; 
    result.firstName = dataSource.firstName; 
    result['lastName'] = dataSource.FunctionThatGetsLastName(); 
} 

注意在JScript中,對象的屬性可以使用object.property符號訪問,也可通過object['property']符號,當你有動態命名的屬性,這是非常有用的。 (在第二個表示法中'property'是一個字符串,可以由任何返回字符串的表達式替換)。

+0

嗯,事情是。我無法使用Scripting.Dictionary,因爲我在創建用戶之前獲取了這些數據。我確實有使用JScript的選項。和PerlScript一樣。 VBscript就是我用過的東西。 – 2011-03-24 12:16:48

+0

當用戶創建時,Dictionary與用戶有什麼關係?你從哪裏獲得數據? – Martijn 2011-03-24 12:21:45

+0

我從名爲Request的參考中獲取數據。它通過稱爲onPreCreate(Request)的事件處理程序自動完成。請求包含正在創建的對象的數據。編輯:看起來像我誤解了Scripting.Dictionary以及 – 2011-03-24 12:29:47

0

保持與VBScript,一個斷開的記錄集將工作以及可能比字典更快;舉例來說,參見here

+0

我嚴重懷疑記錄集會比字典更快。再次,我沒有任何證據支持這一點,也沒有時間來測試它。 – Martijn 2011-03-24 14:03:38