Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need some help with the following block of code:

(roles comes in a populated ArrayList)

 

1 Dim allroles1(100) As String

2 Dim allroles2 As String

3 roles.CopyTo(allroles1)

4 allroles2.Join("|", allroles1)

5 Debug.WriteLine(allroles1(0) & allroles1(1))

6 Debug.WriteLine("allroles2 = " & allroles2)

 

I'd like to convert the inbound ArrayList roles into a string of delimited words in allroles2. Right now allroles2 is coming out blank. I am sure the copyto (3) from the arraylist to the array is working ok because line 5 is printing out the values OK.

 

Any help would be appreciated. Thanks.

  • Leaders
Posted

Instead of copying the arraylist to a new array, you can create a strongly typed array from the arraylist (I think that this is easier), by using the ToArray() function.

 

Also, like the Replace() function, the Join() function is static (Shared in VB), and does not change the string that you call it from, but rather, returns a new string. Watch out for functions like that. That would mean that you should be using

allroles2 = allroles2.Join("|", allroles1)
'-or-
allroles2 = String.Join("|", allroles1)

Both are equivalent but the second syntax is considered better practice because it makes it clear that the function being called is static, i.e. it is not associated with any instance.

 

Function JoinRoles(Roles As ArrayList) As String
   'Have the ArrayList create the array for you
   Dim RolesArray() As String = DirectCast(Roles.ToArray(GetType(String)), String())
   'Use the [b]shared[/b] Join() method to concatenate all the strings
   Dim AllRoles As String = String.Join("|", RolesArray)
   'Output results for debug
   Debug.WriteLine("AllRoles = " & AllRoles)

   Return AllRoles
End Sub

[sIGPIC]e[/sIGPIC]
Posted (edited)

Thanks both of you. The solution ended up being:

 

allroles2 = String.Join("|", allroles1) 

 

I did not understand that before - that these functions results always have to be assigned to something. Makes sense considering that String is an object and join is just a method (my new BIG words - objects and methods)

 

Thank you both very much. Fixing the problem actually fixed several other cookie related issues down the line. Everything is falling in place now.

Edited by patrick24601

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...