7 Replies - 675 Views - Last Post: 11 November 2011 - 10:10 PM Rate Topic: -----

#1 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Help with getting arraylist back to string

Posted 11 November 2011 - 03:49 AM

Hi all i am try to get the items i add to an arraylist (DHHNoneSystemMerger) back to string but with the code i have below i get the out "System.Collections.ArrayList" instead of the items that i added. My Code:

Module WorkingWithTheFiles
    Public DHHNoneSystemMerger As New ArrayList
    Public DHHSystemMerger As New ArrayList
    Public file1 As String = ""
    Public file2 As String = ""
    Public daseyHillRecords As String = ""
    Public systemMerges As String = ""

    Public Sub CAHFiles()
        Dim str1() As String
        Dim str2() As String
        Try
            For Each line In frmCraigavonRecords.rtbFile1.Lines
                str1 = line.Split("\"c)
                If str1(1).StartsWith("DHH") Then
                    daseyHillRecords += str1(0) & "\" & str1(1) & "\" & str1(2) & "\" & str1(3) & "\" & str1(4) & "\" & str1(5) & "\" & str1(6) & "\" & str1(7) & vbNewLine
                    systemMerges += str1(0) & "\" & str1(1) & "\" & str1(2) & "\" & str1(3) & "\" & str1(4) & "\" & str1(5) & "\" & str1(6) & "\" & str1(7) & vbNewLine
                    If line IsNot Nothing Then
                        For Each line2 In frmCraigavonRecords.rtbFile2.Lines
                            str2 = line2.Split("\")
                            If str1(0).ToString = str2(0).ToString Then
                                If str1(1) = str2(1) Then
                                    DHHNoneSystemMerger.Add("\" & str2(1))
                                ElseIf str1(1) <> str2(1) Then
                                    DHHSystemMerger.Add("\" & str2(1))
                                End If
                            End If

                        Next
                        daseyHillRecords = DHHNoneSystemMerger.ToString ' this is where i am am trying to convert the arry list to string
                        systemMerges = DHHNoneSystemMerger.ToString & DHHSystemMerger.ToString
                    End If
                End If
            Next
        Catch ex As IndexOutOfRangeException
            MessageBox.Show(ex.Message)
        Catch ez As Exception
            MessageBox.Show(ez.Message)
        End Try
        Form2.rtbOpenFile3.Text = daseyHillRecords
        Form2.rtbSTH.Text = systemMerges
    End Sub
End Module

This post has been edited by m_wylie85: 11 November 2011 - 03:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with getting arraylist back to string

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 05:29 AM

The ToString() method was not override in ArrayList class to deal with the collected data. What you have to do is to try to get the complete values in it by looping through the items and get them
Was This Post Helpful? 0
  • +
  • -

#3 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 05:35 AM

Hi Smohd I firgured it out, i got it to work i had done what you suggested

code example:

                        For i = 0 To DHHNoneSystemMerger.Count - 1
                            ' Cast to a string.
                            Output = TryCast(DHHNoneSystemMerger.Item(i), String)
                            daseyHillRecords += Output
                        Next i

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 06:09 AM

Glad you figure it out :)
Was This Post Helpful? 0
  • +
  • -

#5 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 03:11 PM

When concatenating strings in a loop it's good practice to use a StringBuilder to append to instead. Since Strings are immutable each time you add to a string you are creating a new one. A performance gain is achieved when the number of concatenations is greater than 5.
Was This Post Helpful? 0
  • +
  • -

#6 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 03:16 PM

please explain more in a sample for code so i can see what you mean thanks for the reply
Was This Post Helpful? 0
  • +
  • -

#7 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 05:14 PM

The StringBuilder class is in the System.Text namespace so you are required to import it System.text Namespace.

Import System.text



Declaration/Instantation:
Dim sbURL as new StringBuilder()



Example of usage:
'This is an example of writing a non-parameterized query using a string builder
'It is better practice to use parameterized queries to help prevent SQL injection, I just used this as an example to demonstrate 
'StringBuilders usefulness

Private Functon GetExpiredCustomersMailingAddressQuery() as String
sbURL.AppendLine("Select cust.FirstName, cust.LastName, addr.Line1, addr.Line2,")
sbUrl.AppendLine("       addr.City, addr.State, addr.Zip")
sbUrl.Appendline("From   Customer cust INNER JOIN Address addr on cust.CustID = addr.CustID")
sbUrl.Appendline("Where  cust.ExpirationDate < '")
sbUrl.Append(DateTime.Now)
sbUrl.AppendLine("'")
sbUrl.AppendLine("Order By cust.LastName desc")

Return sbUrl.ToString
End Function




More Info:
MSDN StringBuilder

Edit: Forgot last code tag, indent tags don't work in Code tags, and added MSDN reference.

This post has been edited by xnn: 11 November 2011 - 05:18 PM

Was This Post Helpful? 1
  • +
  • -

#8 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Help with getting arraylist back to string

Posted 11 November 2011 - 10:10 PM

I realized i named the string builder sbURL. sorry if it's misleading I was originally going to piece together a web address, instead I went with a query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1