locked
Can i use DirectoryEntry.Dispose() when enumerating SearchresultCollection RRS feed

  • Question

  • User-212890919 posted

    Im still using Framework 1.x and have a question about the Dispose() method.

    In the method below i am enumerating the SearchResultCollection and add each item to the CollectionBase ( inherrited) as a user or group or whatever object that the item describes in it's objectClass property.

    Now, foreach SearchResult i enumerate i use SearchResult.GetDirectoryEntry so i can get the properties for the object. Should i in this case Dispose the DirectoryEntry object forech enumeration like the example in red text below.

    public AdObject SearchResultCollectionProperties(SearchResultCollection objSearchResults)

    {

       try

       {

             foreach(SearchResult objResult in objSearchResults)

             {

                   DirectoryEntry objEntry = objResult.GetDirectoryEntry();

                   // set the object type

                   oAdObject=AdObjectType.GetObjectType(objEntry);

                   oAdObject.GetObjectProperties(objEntry);

                   this.List.Add(oAdObject);

                   objEntry.Dispose();

             }

             oAdObject= null;

             if (this.Count > 0)

                {

                   objSearchResults.Dispose();

                   return (AdObject) this.List[0];

                  }

                   else

                   {

                   objSearchResults.Dispose();

                   return null;

                }

                }

    catch (Exception e)

    {

    Error.ErrorMsg("SearchResultCollectionProperties", "Private 11", e);

    return null;

    }

    }

    Friday, June 16, 2006 3:50 AM

All replies

  • User1354132231 posted
    You should definitely Dispose the SearchResultCollection once you are done enumerating it.  You cannot however Dispose the DirectoryEntry objects obtained yet.  If you do this, you cannot use them later.  You should only call Dispose on them when you are done using the collection.  I would suggest perhaps storing the SearchResult in your collection instead of the DirectoryEntry.  As you later enumerate this collection you can use the GetDirectoryEntry method and then call Dispose as necessary assuming you are editing the object.  Creating a whole collection of DirectoryEntry objects that might or might not get edited is probably not as good as just using the SearchResult for read operations.

    Monday, June 19, 2006 1:50 PM
  • User-212890919 posted

    I know this is a late response to Dunnry but i feel forced to thank you for youre tips. I use this method of revealing the properties and store it's values in the collection. Here is one property of many i use in the oAdObject.GetObjectProperties(objEntry) called from  within the enumeration (loop) method to populate the CollectionBase. Having  the objResult i need to call objResult.GetDirectoryEntry() to get all the values i want to have from SearchResult (objResult ) collection i belive. This has worked fine and i stick with it and trust the dispose() of  SearchResultCollection will be all that is needed.

    public override void GetObjectProperties(DirectoryEntry objUserEntry)

    {

          if(userProps.Contains("givenName")!=false)

             {this.givenName = userProps["givenName"][0].ToString().Replace("'","");}

    // and so on

    }

    Thanks

    Monday, June 26, 2006 7:38 AM