• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

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

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
                    isGroupMember = True
                    Exit For
                End If
            Next member
            If (Not isGroupMember) Then
                group.Invoke("Add", New Object() {deUser.Path.ToString()})
            End If
        End If
    End Sub

Open in new window

  • 2
  • 2
1 Solution
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.


Dim group As DirectoryEntry = GetDirectoryEntry()
w33mhzAuthor Commented:
Well it was a copy and paste thing off of a website.  
Also, you should be able to use this method, and remove the function entirely:

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

SearchResult..::.GetDirectoryEntry Method
w33mhzAuthor Commented:
ok thank you I will try that.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now