Results 1 to 3 of 3

Thread: [2005] Continued from previous thread....array or arrayList

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    [2005] Continued from previous thread....array or arrayList

    Continuing from a previous thread (http://www.vbforums.com/showthread.php?&t=429114) i came across a little problem. When copying the elements of the previous array into the new one i noticed that its copying all the elements that have nothing in them, so instead of adding, for example, one element across each time, its copying 2 and i have a load of elements in my new array that have nothing in them. So how can i change the code in posted in the previous thread so that only the element that contains something is copied?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Continued from previous thread....array or arrayList

    Huh? How is it copying two elements at a time? I think you need to give an example of what each array contains before the operation and then what the resized array contain afterwards, and show EXACTLY what code you're using. I used this code:
    VB Code:
    1. Dim existingArray As String() = {"String1", "String2", "String3"}
    2. Dim arrayToAdd As String() = {"String4", "String5", "String6"}
    3.  
    4. Dim originalLength As Integer = existingArray.Length
    5.  
    6. Array.Resize(existingArray, originalLength + arrayToAdd.Length)
    7. arrayToAdd.CopyTo(existingArray, originalLength)
    8.  
    9. MessageBox.Show(String.Join(", ", existingArray))
    and it produced the result below, exactly as you'd expect.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Continued from previous thread....array or arrayList

    I think its because i am doing this all on a separate thread which is controlled by a timer, so everytime the timer ticks a new XML file is downloaded and the information is stored in arr_Results.
    Here's the code i have anyway just for reference:
    VB Code:
    1. Private Sub tmr_Thread_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_Thread.Tick
    2.         threadTick = True
    3.         Dim thread1 As New Thread(New ThreadStart(AddressOf Me.getResults))
    4.         thread1.Start() ' start results thread
    5.     End Sub
    6.  
    7.     Private Sub getResults()
    8.         copy_web_to_local(destination folder, "web_address_of_XML_file", "XML_file.xml")
    9.         getResultsXML(CACHE_DIRECTORY & "results.xml")
    10.         createTxtResults()
    11.     End Sub
    12.  
    13. Public Sub getResultsXML(ByVal _file As String)
    14.         '
    15.         Try
    16.             xml_doc = New XmlDocument()                             'Create the XML Document
    17.             xml_doc.Load(_file)                                     'Load the Xml file
    18.             xml_nodelist = xml_doc.SelectNodes("/results/result")     'Get the list of name nodes
    19.             xml_length = xml_nodelist.Count()                       'Length of xml_nodelist
    20.             '
    21.             i = 0
    22.             '
    23.             For Each xml_node In xml_nodelist                       'Loop through the nodes
    24.                 '      
    25.                 Dim att_time = xml_node.ChildNodes.Item(0).InnerText
    26.                 Dim att_name = xml_node.ChildNodes.Item(1).InnerText
    27.                 Dim att_method = xml_node.ChildNodes.Item(2).InnerText
    28.                 '
    29.                 arr_Results(i).time = att_time
    30.                 arr_Results(i).name = att_name
    31.                 arr_Results(i).method = att_method
    32.  
    33.                 ReDim Preserve arr_Results(i + 1)
    34.                 i += 1
    35.             Next
    36.             originalLength = arr_storeResults.Length
    37.             Array.Resize(arr_storeResults, originalLength + arr_Results.Length)
    38.             arr_Results.CopyTo(arr_storeResults, originalLength)
    39.  
    40.         Catch webEx As System.Net.WebException
    41.             MsgBox("Web Exception caught in FileFromWeb.copy_web_to_local()" & NL & "Reason: " & webEx.ToString())
    42.         Catch ex As Exception
    43.             MsgBox("Exception caught in FileFromWeb.copy_web_to_local()" & NL & "Reason: " & ex.ToString())
    44.         End Try
    45.         '
    46.     End Sub
    and the when the timer has ticked twice the content of arr_storeResults are:
    arr_storeResults(0) = nothing
    arr_storeResults(1) = result
    arr_storeResults(2) = nothing
    arr_storeResults(3) = result
    arr_storeResults(4) = nothing etc...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width