/// <summary>
/// Returns the next result as an LdapEntry.
/// If automatic referral following is disabled or if a referral
/// was not followed, next() will throw an LdapReferralException
/// when the referral is received.
/// </summary>
/// <returns>
/// The next search result as an LdapEntry.
/// </returns>
/// <exception>
/// LdapException A general exception which includes an error
/// message and an Ldap error code.
/// </exception>
/// <exception>
/// LdapReferralException A referral was received and not
/// followed.
/// </exception>
public virtual LdapEntry next()
{
if (completed && entryIndex >= entryCount && referenceIndex >= referenceCount)
{
throw new ArgumentOutOfRangeException("LdapSearchResults.next() no more results");
}
// Check if the enumeration is empty and must be reloaded
resetVectors();
object element = null;
// Check for Search References & deliver to app as they come in
// We only get here if not following referrals/references
if (referenceIndex < referenceCount)
{
var refs = (string[])references[referenceIndex++];
var rex = new LdapReferralException(ExceptionMessages.REFERENCE_NOFOLLOW);
rex.setReferrals(refs);
throw rex;
}
if (entryIndex < entryCount)
{
// Check for Search Entries and the Search Result
element = entries[entryIndex++];
if (element is LdapResponse)
{
// Search done w/bad status
if (((LdapResponse)element).hasException())
{
var lr = (LdapResponse)element;
var ri = lr.ActiveReferral;
if (ri != null)
{
// Error attempting to follow a search continuation reference
var rex = new LdapReferralException(ExceptionMessages.REFERENCE_ERROR, lr.Exception);
rex.setReferrals(ri.ReferralList);
rex.FailedReferral = ri.ReferralUrl.ToString();
throw rex;
}
}
// Throw an exception if not success
((LdapResponse)element).chkResultCode();
}
else if (element is LdapException)
{
throw (LdapException)element;
}
}
else
{
// If not a Search Entry, Search Result, or search continuation
// we are very confused.
// LdapSearchResults.next(): No entry found & request is not complete
throw new LdapException(ExceptionMessages.REFERRAL_LOCAL, new object[] { "next" },
LdapException.LOCAL_ERROR, null);
}
return((LdapEntry)element);
}