patrick24601 Posted June 15, 2005 Posted June 15, 2005 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. Quote
Administrators PlausiblyDamp Posted June 15, 2005 Administrators Posted June 15, 2005 When doing this the .Join method returns the delimited string. Try something like Dim AllRoles2 As String AllRoles2 = String.Join("|", Roles.ToArray(GetType(String))) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted June 15, 2005 Leaders Posted June 15, 2005 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 Quote [sIGPIC]e[/sIGPIC]
patrick24601 Posted June 15, 2005 Author Posted June 15, 2005 (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 June 15, 2005 by patrick24601 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.