Gitcho
asked on
How to return an array/dict.obj from function
1. what's the best method to use ? array or dictionary object
2. how would I code it (see function below) to add the name of each computer to the array
3. how would I loop thru the array once the function returns it ?
Function getHostNameFromUsername(sU sername)
Dim objUser,userClass,adsCompu ter,adsSes sion,HomeD ir,dPClist
On Error Resume Next
Set objUser= GetObject("WinNT://Domain/ " & sUserName & ",user")
Set userClass = GetObject(objUser.Schema)
' ************************** ********** ********** ********** ********** ****
' Return false if bad username
' ************************** ********** ********** ********** ********** ****
If objUser.name = "" Then
getHostNameFromUsername = False
End If
' ************************** ********** ********** ********** ********** ****
' Get Logon server from profile dir
' ************************** ********** ********** ********** ********** ****
Dim pos1,pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec tory, "\")
HomeDir = Mid(objUser.HomeDirectory, 3,pos1-3)
' ************************** ********** ********** ********** ********** ****
' Identifying which sessions exist on a machine
' ************************** ********** ********** ********** ********** ****
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
For Each adsSession In adsComputer.Sessions
*** Want to add the name of each computer a user is logged onto to array ****
*** code to add "adsSession.Computer" to [whatever_array] goes here ***
Next
On Error Goto 0
*** Want to return the array of computers a user is logged onto ****
getHostNameFromUsername = [whatever_array]
End Function
2. how would I code it (see function below) to add the name of each computer to the array
3. how would I loop thru the array once the function returns it ?
Function getHostNameFromUsername(sU
Dim objUser,userClass,adsCompu
On Error Resume Next
Set objUser= GetObject("WinNT://Domain/
Set userClass = GetObject(objUser.Schema)
' **************************
' Return false if bad username
' **************************
If objUser.name = "" Then
getHostNameFromUsername = False
End If
' **************************
' Get Logon server from profile dir
' **************************
Dim pos1,pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec
HomeDir = Mid(objUser.HomeDirectory,
' **************************
' Identifying which sessions exist on a machine
' **************************
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
For Each adsSession In adsComputer.Sessions
*** Want to add the name of each computer a user is logged onto to array ****
*** code to add "adsSession.Computer" to [whatever_array] goes here ***
Next
On Error Goto 0
*** Want to return the array of computers a user is logged onto ****
getHostNameFromUsername = [whatever_array]
End Function
To loop through the collection:
Private Sub Command1_Click()
Dim MyComputers As Scripting.Dictionary
Dim i As Long
Set MyComputers = getHostNameFromUsername()
With MyComputers
For i = 0 To .Count - 1
MsgBox .Items(i)
Next i
End With
End Sub
Private Sub Command1_Click()
Dim MyComputers As Scripting.Dictionary
Dim i As Long
Set MyComputers = getHostNameFromUsername()
With MyComputers
For i = 0 To .Count - 1
MsgBox .Items(i)
Next i
End With
End Sub
To do it using an array:
Function getHostNameFromUsername(sU sername) As String()
Dim objUser, userClass, adsComputer, adsSession, HomeDir, dPClist
Dim arrComputers() As String
On Error Resume Next
Set objUser = GetObject("WinNT://Domain/ " & sUsername & ",user")
Set userClass = GetObject(objUser.Schema)
' ************************** ********** ********** ********** ********** ****
' Return false if bad username
' ************************** ********** ********** ********** ********** ****
If objUser.Name = "" Then
'getHostNameFromUsername = False
End If
' ************************** ********** ********** ********** ********** ****
' Get Logon server from profile dir
' ************************** ********** ********** ********** ********** ****
Dim pos1, pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec tory, "\")
HomeDir = Mid(objUser.HomeDirectory, 3, pos1 - 3)
' ************************** ********** ********** ********** ********** ****
' Identifying which sessions exist on a machine
' ************************** ********** ********** ********** ********** ****
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
Dim i As Long
i = 0
For Each adsSession In adsComputer.Sessions
ReDim Preserve arrComputers(i)
arrComputers(i) = CStr(adsSession.Computer)
'*** Want to add the name of each computer a user is logged onto to array ****
'*** code to add "adsSession.Computer" to [whatever_array] goes here ***
Next
On Error GoTo 0
'*** Want to return the array of computers a user is logged onto ****
getHostNameFromUsername = arrComputers
End Function
Private Sub Command1_Click()
Dim arrComputers() As String
Dim i As Long
arrComputers = getHostNameFromUsername()
For i = LBound(arrComputers) To UBound(arrComputers)
MsgBox arrComputers(i)
Next i
End Sub
Function getHostNameFromUsername(sU
Dim objUser, userClass, adsComputer, adsSession, HomeDir, dPClist
Dim arrComputers() As String
On Error Resume Next
Set objUser = GetObject("WinNT://Domain/
Set userClass = GetObject(objUser.Schema)
' **************************
' Return false if bad username
' **************************
If objUser.Name = "" Then
'getHostNameFromUsername = False
End If
' **************************
' Get Logon server from profile dir
' **************************
Dim pos1, pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec
HomeDir = Mid(objUser.HomeDirectory,
' **************************
' Identifying which sessions exist on a machine
' **************************
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
Dim i As Long
i = 0
For Each adsSession In adsComputer.Sessions
ReDim Preserve arrComputers(i)
arrComputers(i) = CStr(adsSession.Computer)
'*** Want to add the name of each computer a user is logged onto to array ****
'*** code to add "adsSession.Computer" to [whatever_array] goes here ***
Next
On Error GoTo 0
'*** Want to return the array of computers a user is logged onto ****
getHostNameFromUsername = arrComputers
End Function
Private Sub Command1_Click()
Dim arrComputers() As String
Dim i As Long
arrComputers = getHostNameFromUsername()
For i = LBound(arrComputers) To UBound(arrComputers)
MsgBox arrComputers(i)
Next i
End Sub
ASKER
Good stuff ... what's your recommendation - dictionary or array ? What does best practises say to use ?
ASKER
Shoot ... sorry ... forgot to mention this is for an ASP page ... doesn't look like you can typecast "Dim arrComputers() As String" in vbscript ...
Any way you can still make that work ?
Any way you can still make that work ?
I would probably use an array in this circumstance as they are simpler. But that's not to say there's anything wrong with using a dictionary object.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This was the code that ended up working on my page ... thanks ...
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
Function getHostNameFromUsername(sU sername)
Dim objUser,userClass,adsCompu ter,adsSes sion,HomeD ir,colComp uters
Set objUser= GetObject("WinNT://Domain/ " & sUserName & ",user")
Set userClass = GetObject(objUser.Schema)
' ************************** ********** ********** ********** ********** ****
' Return false if bad username
' ************************** ********** ********** ********** ********** ****
If objUser.name = "" Then
getHostNameFromUsername = False
End If
' ************************** ********** ********** ********** ********** ****
' Get Logon server from profile dir
' ************************** ********** ********** ********** ********** ****
Dim pos1,pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec tory, "\")
HomeDir = Mid(objUser.HomeDirectory, 3,pos1-3)
' ************************** ********** ********** ********** ********** ****
' Identifying which sessions exist on a machine
' ************************** ********** ********** ********** ********** ****
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
Set colComputers = CreateObject("Scripting.Di ctionary")
For Each adsSession In adsComputer.Sessions
If uCase(adsSession.User) = uCase(sUsername) Then
colComputers.Add CStr(adsSession.Computer), CStr(adsSession.Computer)
End If
Next
Set getHostNameFromUsername = colComputers
End Function
--------------------------
Function getHostNameFromUsername(sU
Dim objUser,userClass,adsCompu
Set objUser= GetObject("WinNT://Domain/
Set userClass = GetObject(objUser.Schema)
' **************************
' Return false if bad username
' **************************
If objUser.name = "" Then
getHostNameFromUsername = False
End If
' **************************
' Get Logon server from profile dir
' **************************
Dim pos1,pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec
HomeDir = Mid(objUser.HomeDirectory,
' **************************
' Identifying which sessions exist on a machine
' **************************
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
Set colComputers = CreateObject("Scripting.Di
For Each adsSession In adsComputer.Sessions
If uCase(adsSession.User) = uCase(sUsername) Then
colComputers.Add CStr(adsSession.Computer),
End If
Next
Set getHostNameFromUsername = colComputers
End Function
The function has been changed to return a Scripting.Dictionary object so setting the reult to false has been commented out. You could always passs the scripting dicrionary reference as a parameter rather than the return valu of the function.
Option Explicit
Function getHostNameFromUsername(sU
Dim objUser, userClass, adsComputer, adsSession, HomeDir, dPClist
Dim colComputers As Scripting.Dictionary
On Error Resume Next
Set objUser = GetObject("WinNT://Domain/
Set userClass = GetObject(objUser.Schema)
' **************************
' Return false if bad username
' **************************
If objUser.Name = "" Then
'getHostNameFromUsername = False
End If
' **************************
' Get Logon server from profile dir
' **************************
Dim pos1, pos2
HomeDir = objUser.HomeDirectory
pos1 = InStrRev(objUser.HomeDirec
HomeDir = Mid(objUser.HomeDirectory,
' **************************
' Identifying which sessions exist on a machine
' **************************
Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
Set colComputers = New Scripting.Dictionary
For Each adsSession In adsComputer.Sessions
colComputers.Add CStr(adsSession.Computer),
'*** Want to add the name of each computer a user is logged onto to array ****
'*** code to add "adsSession.Computer" to [whatever_array] goes here ***
Next
On Error GoTo 0
'*** Want to return the array of computers a user is logged onto ****
Set getHostNameFromUsername = colComputers
End Function