Link to home
Start Free TrialLog in
Avatar of w33mhz
w33mhz

asked on

"Cannot be indexed because it has no default value" error

Hello,
Please bear with me, I am very new to VB .NET, most of my experience is with vbscript and I get confused real quick in  .NET.

I have a Sub that I found to add an active directory user to a group, but I get an error "Class 'System.DirectoryServices.DirectoryEntry' cannot be indexed because it has no default value"  It gives me this on "Dim group As DirectoryEntry = GetDirectoryEntry(results(0).Path)".  GetDirectoryEntry is a function I created that looks like this:

Public Shared Function GetDirectoryEntry() As DirectoryEntry
        Dim de As DirectoryEntry = New DirectoryEntry("LDAP://tmctrans.com")
        de.AuthenticationType = AuthenticationTypes.Secure
        Return de
    End Function
What do I need to do to correct this?
Public Shared Sub AddUserToGroup(ByVal de As DirectoryEntry, ByVal deUser As DirectoryEntry, ByVal GroupName As String)
        Dim deSearch As DirectorySearcher = New DirectorySearcher()
        deSearch.SearchRoot = de
        deSearch.Filter = "(&(objectClass=group) (cn=" & GroupName & "))"
        Dim results As SearchResultCollection = deSearch.FindAll()
        Dim isGroupMember As Boolean = False
        If results.Count > 0 Then
            Dim group As DirectoryEntry = GetDirectoryEntry(results(0).Path)
            Dim members As Object = group.Invoke("Members", Nothing)
            For Each member As Object In CType(members, IEnumerable)
                Dim x As DirectoryEntry = New DirectoryEntry(member)
                Dim name As String = x.Name
                If name <> deUser.Name Then
                    isGroupMember = False
                Else
                    isGroupMember = True
                    Exit For
                End If
            Next member
            If (Not isGroupMember) Then
                group.Invoke("Add", New Object() {deUser.Path.ToString()})
            End If
            group.Close()
        End If
        Return
    End Sub

Open in new window

Avatar of PaulHews
PaulHews
Flag of Canada image

Dim group As DirectoryEntry = GetDirectoryEntry(results(0).Path)

Why are you passing a parameter to this function, when the definition doesn't call for one.

Try:

Dim group As DirectoryEntry = GetDirectoryEntry()
Avatar of w33mhz
w33mhz

ASKER

Well it was a copy and paste thing off of a website.  
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of w33mhz

ASKER

ok thank you I will try that.