locked
Reversing the order of an Array or String RRS feed

  • Question

  • User-1031976281 posted

    Im looking to dynamically populate a Placeholder with data sent from a textbox on the same page, but in reverse order i.e. the latest post goes to the top ratehr than the bottom - kidda like Twitter or Facebook.

    The code I have so far is below but I keep geeting an error stating that "BC30311: Value of type 'System.Collections.ArrayList' cannot be converted to 'System.Array'."

    Please help....

     

        Protected Sub AddNewWordButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewWordButton.Click
    
            Dim DictionaryArrayList As ArrayList = New ArrayList()
            If (Me.ViewState("words") IsNot Nothing) Then
                DictionaryArrayList = ViewState("words")
            End If
    
            Dim newword As String = AddNewWordTextBox.Text
            DictionaryArrayList.Add(newword)
            ViewState("words") = DictionaryArrayList
            DynamicDisplay()
    
        End Sub
    
        Public Sub DynamicDisplay()
    
            Dim DictionaryArrayList As ArrayList = New ArrayList()
            DictionaryArrayList = ViewState("words")
    
            For x = 0 To DictionaryArrayList.Count - 1
    
                Dim holder As New PlaceHolder
                holder.Controls.Add(New LiteralControl("(" & x + 1 & ") "))
                holder.Controls.Add(New LiteralControl(DictionaryArrayList(x).ToString()))
                Array.Reverse(DictionaryArrayList)
                holder.Controls.Add(New LiteralControl("   "))
    
                Dim theDeleteButton As Button = New Button()
                theDeleteButton.ID = "deletebutton" & x.ToString()
                theDeleteButton.Text = "X"
                theDeleteButton.Attributes.Add("OnClick", "DeleteButtonClick();")
                holder.Controls.Add(theDeleteButton)
                holder.Controls.Add(New LiteralControl("<br />"))
                dictionaryplaceholder.Controls.Add(holder)
                AddNewWordTextBox.Text = String.Empty
            Next
    
        End Sub


     

    Tuesday, June 29, 2010 9:39 AM

Answers

  • User-1618234021 posted

    Use the following code to reverse the arraylist

    ArrayList al = new ArrayList();
                al.Add("Adeel");
                al.Add("Ehsan");
                object[] ob = al.ToArray();
                Array.Reverse(ob);
                foreach (object o in ob)
                {
                    Response.Write(o.ToString() + "<br>");
                }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 30, 2010 1:54 AM
  • User-1509636757 posted

    However, I can't see how I can control the positioning, the text szie, font, etc

    i understand that you have data ready and you need to control on positioning, text size and other design things..

    what you can do is have an html format (possibly in a .htm file) with required tokens in it (like %firstName%, %lastName%) for you data .. read that file in above for loop, replace the data with tokens and create a string like that.. finally write that string to response object..

    hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 2, 2010 6:44 AM
  • User-1802908944 posted

    use it in vb.net

    Function Reverse(ByVal value As String) As String
            ' Convert to char array.
            Dim arr() As Char = value.ToCharArray()
            ' Use Array.Reverse function.
            Array.Reverse(arr)
            ' Construct new string.
            Return New String(arr)
        End Function

    happy coding

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 3, 2010 3:00 AM

All replies

  • User-1618234021 posted

    Modify DynamicDisplay sub as following:

    Public Sub DynamicDisplay()
    
            Dim DictionaryArrayList As ArrayList = New ArrayList()
            DictionaryArrayList = ViewState("words")
    
            For x = 0 To DictionaryArrayList.Count - 1
    
                Dim holder As New PlaceHolder
                holder.Controls.Add(New LiteralControl("(" & x + 1 & ") "))
                holder.Controls.Add(New LiteralControl(DictionaryArrayList(x).ToString()))
                'Use ToArray() to convert ArrayList to Array
                 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                Array.Reverse(DictionaryArrayList.ToArray())
                holder.Controls.Add(New LiteralControl("   "))
    
                Dim theDeleteButton As Button = New Button()
                theDeleteButton.ID = "deletebutton" & x.ToString()
                theDeleteButton.Text = "X"
                theDeleteButton.Attributes.Add("OnClick", "DeleteButtonClick();")
                holder.Controls.Add(theDeleteButton)
                holder.Controls.Add(New LiteralControl("<br />"))
                dictionaryplaceholder.Controls.Add(holder)
                AddNewWordTextBox.Text = String.Empty
            Next
    
        End Sub



    Tuesday, June 29, 2010 10:36 AM
  • User-1031976281 posted

    Hi,

    Thanks for this, but it still doesn't work.

     

    Although the code will now compile, it doesn't reverse the order of the array..

    Tuesday, June 29, 2010 10:55 AM
  • User-1618234021 posted

    Use the following code to reverse the arraylist

    ArrayList al = new ArrayList();
                al.Add("Adeel");
                al.Add("Ehsan");
                object[] ob = al.ToArray();
                Array.Reverse(ob);
                foreach (object o in ob)
                {
                    Response.Write(o.ToString() + "<br>");
                }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 30, 2010 1:54 AM
  • User-1031976281 posted

    Hi, Thanks for this it works a treat once I converted it to VB.

     

    However, I can't see how I can control the positioning, the text szie, font, etc

     

    Please Help! :) 

    Friday, July 2, 2010 6:16 AM
  • User-1509636757 posted

    However, I can't see how I can control the positioning, the text szie, font, etc

    i understand that you have data ready and you need to control on positioning, text size and other design things..

    what you can do is have an html format (possibly in a .htm file) with required tokens in it (like %firstName%, %lastName%) for you data .. read that file in above for loop, replace the data with tokens and create a string like that.. finally write that string to response object..

    hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 2, 2010 6:44 AM
  • User-1802908944 posted

    use it in vb.net

    Function Reverse(ByVal value As String) As String
            ' Convert to char array.
            Dim arr() As Char = value.ToCharArray()
            ' Use Array.Reverse function.
            Array.Reverse(arr)
            ' Construct new string.
            Return New String(arr)
        End Function

    happy coding

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 3, 2010 3:00 AM
  • User-1031976281 posted

    Thanks for your help you guys - I eventually stumbled upon the following code which has reversed the output for me....

     

      For x As Integer = DictionaryArrayList.Count - 1 To 0 Step -1


     

    All I needed to do was replace the following line with the one above

    For x = 0 To DictionaryArrayList.Count - 1


     

    Tuesday, July 13, 2010 11:30 AM