从函数调用返回多个值

时间:2010-04-01 08:29:50

标签: asp.net vb.net function syntax

我有一个活动的目录搜索功能:

    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

该函数似乎有效,因为当我在末尾放置断点时,ADDN等变量的值确实正确。

然后我在我的page_load中调用这个函数:

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

然后我尝试回复。写出其中一个变量来测试这样:

Response.Write(ADDN) 

但价值是空的。

请有人告诉我我做错了什么。感谢

1 个答案:

答案 0 :(得分:3)

你只能从一个函数返回一个值 - 你可以通过引用传递这些值,它们应该返回正确的值,但对我来说,我认为你最好用你需要的属性创建一个小类返回,并从此方法返回该类的实例,填充返回的变量 - 这意味着如果必须添加更多返回值,则更改此方法的问题会更少。