how do I get the key for a collection?
If I have the following VB.NET collection:
Dim oUser As New Collection
oUser.Add("jbloggs", "CN")
oUser.Add("jbloggs", "samaccountname")
oUser.Add("Joe Bloggs", "DisplayName")
oUser.Add("Joe", "givenName")
oUser.Add("Bloggs", "sn")
I can do this:
msgbox (oUser("CN"))
and get this result: "jbloggs"
But how would I check for the collection's key = "CN". It would be handy if the Key could be used in conjunction with its value as well. Example (only pseudo code, this will not actually work)
For i = 1 to oUser.Count
If oUser(i).Key = "CN" Then
....
Next
I figured out how to do it, but I had to use:
| |
Dim oUser As New System.Collections.Specialized.NameValueCollection |
Instead of
| |
Dim oUser As New Collection |
Using a collection that provides access to the key and value makes it pretty easy to create active directory accounts. Particularly so if you cannot always guarantee the number of attributes that will be supplied.
Example:
| |
Public Function CreateADUser(ByVal oUserCollection As System.Collections.Specialized.NameValueCollection) As Boolean Dim bOK As Boolean = True Dim dom As New System.DirectoryServices.DirectoryEntry Dim i As Integer Try Dim ou As System.DirectoryServices.DirectoryEntry = dom.Children.Find("CN=USERS") Dim oUser As System.DirectoryServices.DirectoryEntry = ou.Children.Add("CN=" & oUserCollection("CN"), "user") For i = 0 To oUserCollection.Count - 1 If oUserCollection.AllKeys(i) <> "CN" Then oUser.Properties(oUserCollection.AllKeys(i)).Value = oUserCollection(i) End If Next oUser.CommitChanges() Catch ex As Exception msgbox (ex.message) bOK = False End Try Return bOK End Function |