Casting Problem returning an ArrayList

P

Paul D. Fox

I am getting a casting issue when trying to get the results back from a web
service. Its returning an ArrayList. Anyone know how to correct this?

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click

'Instantiate the Web Service
Dim GetListOfUsersADsGroupMemberships As New
wsActiveDirectoryWebReference.wsActiveDirectory
Dim arrListOfUsersGroupMemberships As ArrayList
Try

arrListOfUsersGroupMemberships =
GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)

arrListOfUsersGroupMemberships.Sort()
GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
Catch ex As Exception
Response.Write("An exception has occurred. Please verify LoginID.")
Finally

End Try

End Sub
 
D

Dan Rogers

Hi Paul,

On the calling side, assuming you used add-web-reference, there will be an
array of some type (look at the proxy code) returned and not an array list.

This is because on the wire you cannot distinguish the two.

I hope this helps

Dan Rogers
Microsoft Corporation
--------------------
 
P

Paul D. Fox

Hmmm,

When I look at the proxy code, it seems to be returning an "Object". Can I
then cast it as an ArrayList at this point? Currently I get the error
message that says "Value of type '1-dimensional array of System.Object'
cannot be converted to 'system.Collections.ArrayList'.


Here is my webMethod:
'***************************************************************************
********
'* Web Service Method: ADsListUsersGroups
'* Description: This function will return the Groups a particluar AD
User is
'* currently a member of.
'*
'* Input: LoginID As String
'* Output: ArrayList
'***************************************************************************
********
<WebMethod(Description:="This obtains a list of groups the user is a member
of in Active Directory. Excepts LoginID As String, Returns an ArrayList of
Groups the user is a member of.")> _
Public Function ADsListUsersGroups(ByVal strLoginID As String) As
ArrayList
Dim ADsEntry1 As DirectoryEntry
Dim arrGroupMemberships As New ArrayList
'Dim arrGroupMemberships As Array

Try
ADsEntry1 = New DirectoryEntry(_DOMAIN,
ConfigurationSettings.AppSettings("ADSI_AuthenticationID"),
ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
Dim ADsSearch As DirectorySearcher = New
DirectorySearcher(ADsEntry1)

ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" &
strLoginID & "*))"
Dim ADsResult As SearchResult = ADsSearch.FindOne

Dim i As Integer
Dim iCount As Integer = ADsResult.Properties("MemberOf").Count
If iCount > 0 Then
'Retrieve group membership from Windows ADs and add to
arraylist
For i = 0 To iCount - 1
Dim gADs As String =
ADsResult.Properties("MemberOf").Item(i)
Dim myGroup As String = Left(gADs, (InStr(gADs, ",") -
1))
arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
'arrGroupMemberships.SetValue(myGroup.Replace("CN=",
""), i)
Next
End If
Return arrGroupMemberships
Catch ex As Exception
Throw New System.Exception("An exception has occurred." & vbCrLf &
Err.Number & " - " & Err.Description)
Finally
End Try
End Function

Paul
 
P

Paul D. Fox

Yes, the proxy code says its returning an Object (When in fact the web
service is suppose to be returning an ArrayList). In my calling code I get
an error that says "Value of type '1-dimensional array of System.Object'
cannot be converted to 'Systems.Collections.ArrayList'"

Paul
 
P

Paul D. Fox

Found the issue, although I don't quite understand why since an ArrayList is serializable. What I had to do was to cast the ArrayList in the WebService's return to an Array. See the code below
'***********************************************************************************
'* Web Service Method: ADsListUsersGroups
'* Description: This function will return the Groups a particluar AD User is
'* currently a member of.
'*
'* Input: LoginID As String
'* Output: ArrayList
'***********************************************************************************

<WebMethod(Description:="This obtains a list of groups the user is a member of in Active Directory. Excepts LoginID As String, Returns an ArrayList of Groups the user is a member of.")> _

Public Function ADsListUsersGroups(ByVal strLoginID As String) As Array

Dim ADsEntry1 As DirectoryEntry
Dim arrGroupMemberships As New ArrayList

Try

ADsEntry1 = New DirectoryEntry(_DOMAIN, ConfigurationSettings.AppSettings("ADSI_AuthenticationID"), ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
Dim ADsSearch As DirectorySearcher = New DirectorySearcher(ADsEntry1)
ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & strLoginID & "*))"

Dim ADsResult As SearchResult = ADsSearch.FindOne
Dim i As Integer
Dim iCount As Integer = ADsResult.Properties("MemberOf").Count

If iCount > 0 Then
'Retrieve group membership from Windows ADs and add to arraylist
For i = 0 To iCount - 1
Dim gADs As String = ADsResult.Properties("MemberOf").Item(i)
Dim myGroup As String = Left(gADs, (InStr(gADs, ",") - 1))

arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
Next
End If

Return arrGroupMemberships.ToArray

Catch ex As Exception
Throw New System.Exception("An exception has occurred." & vbCrLf & Err.Number & " - " & Err.Description)
Finally

End Try

End Function



Paul
 
D

Dan Rogers

Great.

You need to remember that the type is not what comes over the wire. It is
the representation of the repeating data that has every appearance of
array. there are ways to make your proxy programming model use array
lists, but this calls for a container around your repeating elements being
represented on the wire.

Dan
--------------------
From: "Paul D. Fox" <[email protected]>
References: <#[email protected]>
Subject: Re: Casting Problem returning an ArrayList
Date: Fri, 10 Dec 2004 09:50:01 -0500
Lines: 349
Organization: Ascii Technologies
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0009_01C4DE9D.9E38C990"
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
NNTP-Posting-Host: 208.49.232.75
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!new
sfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTNGP08.ph
x.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.aspnet.webservices:27128
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

Found the issue, although I don't quite understand why since an ArrayList
is serializable. What I had to do was to cast the ArrayList in the
WebService's return to an Array. See the code below
'************************************************************************** *********
'* Web Service Method: ADsListUsersGroups
'* Description: This function will return the Groups a particluar AD User is
'* currently a member of.
'*
'* Input: LoginID As String
'* Output: ArrayList
'************************************************************************** *********
<WebMethod(Description:="This obtains a list of groups the user is a
member of in Active Directory. Excepts LoginID As String, Returns an
ArrayList of Groups the user is a member of.")> _
Public Function ADsListUsersGroups(ByVal strLoginID As String) As Array
Dim ADsEntry1 As DirectoryEntry
Dim arrGroupMemberships As New ArrayList
Try
ADsEntry1 = New DirectoryEntry(_DOMAIN, ConfigurationSettings.AppSettings("ADSI_AuthenticationID"),
ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
Dim ADsSearch As DirectorySearcher = New DirectorySearcher(ADsEntry1)
ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & strLoginID & "*))"
Dim ADsResult As SearchResult = ADsSearch.FindOne
Dim i As Integer
Dim iCount As Integer = ADsResult.Properties("MemberOf").Count
If iCount > 0 Then
'Retrieve group membership from Windows ADs and add to arraylist
For i = 0 To iCount - 1
Dim gADs As String = ADsResult.Properties("MemberOf").Item(i)
Dim myGroup As String = Left(gADs, (InStr(gADs, ",") - 1))

arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
Next
End If
Return arrGroupMemberships.ToArray
Catch ex As Exception
Throw New System.Exception("An exception has occurred." & vbCrLf &
Err.Number & " - " & Err.Description)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top