views:

28

answers:

1

I have an active directory search function:

    Function GetAdInfo(ByVal ADDN As String, ByVal ADCommonName As String, ByVal ADGivenName As String, ByVal ADStaffNum As String, ByVal ADEmail As String, ByVal ADDescription As String, ByVal ADTelephone As String, ByVal ADOffice As String, ByVal ADEmployeeID As String) As String
        Dim netBIOSname As String = Me.Request.LogonUserIdentity.Name
        Dim sAMAccountName As String = netBIOSname.Substring(netBIOSname.LastIndexOf("\"c) + 1)
        Dim defaultNamingContext As String
        Using rootDSE As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            defaultNamingContext = rootDSE.Properties("defaultNamingContext").Value.ToString()
        End Using
        Using searchRoot As DirectoryServices.DirectoryEntry = _
                            New DirectoryServices.DirectoryEntry("LDAP://" + defaultNamingContext, _
                                "kingkong", "kingkong", DirectoryServices.AuthenticationTypes.Secure)
            Using ds As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(searchRoot)
                ds.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", sAMAccountName)
                Dim sr As DirectoryServices.SearchResult = ds.FindOne()

                ADDN = (sr.Properties("displayName")(0).ToString())
                ADCommonName = (sr.Properties("cn")(0).ToString())
                ADGivenName = (sr.Properties("givenname")(0).ToString())
                ADStaffNum = (sr.Properties("sn")(0).ToString())
                ADEmail = (sr.Properties("mail")(0).ToString())
                ADDescription = (sr.Properties("description")(0).ToString())
                ADTelephone = (sr.Properties("telephonenumber")(0).ToString())
                ADOffice = (sr.Properties("physicalDeliveryOfficeName")(0).ToString())

            End Using
        End Using
        Return ADDN
        Return ADCommonName
        Return ADGivenName
        Return ADStaffNum
        Return ADEmail
        Return ADDescription
        Return ADTelephone
        Return ADOffice

The function appears to work, as when I put a breakpoint at the end, the variables such as ADDN do have the correct values.

Then I call the function in my page_load like this:

GetAdInfo(ADDN, ADCommonName, ADGivenName, ADStaffnum, ADEmail, ADDescription, ADTelephone, ADOffice, ADEmployeeID)

Then I try to response.write out one of the vars to test like this:

Response.Write(ADDN) 

But the value is empty.

Please can someone tell me what I am doing wrong. Thanks

+2  A: 

You can only return a single value from a function - you can pass these by reference and they should come back with the correct values, but for me, I think you'd be better creating a small class with the properties you need returned, and return an instance of that class from this method, populated with the returned variables - this means you'll have fewer problems changing this method if you have to add more return values.

Paddy
When you say pass them by reference do you mean like this: ADGivenName = (sr.Properties("givenname")(0).ToString()) and remove all the Return 's ?
Phil
I mean change the byval to a byref, although I really wouldn't recommend this as a solution.
Paddy