Results 1 to 6 of 6

Thread: [RESOLVED] Replacing Elements of An Array with Elements from Another Array (VB.NET)

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    7

    Resolved [RESOLVED] Replacing Elements of An Array with Elements from Another Array (VB.NET)

    Hello bosses!
    I created arrays of string values in two textboxes. For each element of the array in Textbox2, I want to loop through Textbox1 to find if there is any element in textbox1 that is completely contained in any of the Textbox2 array elements. If there is any such situation(there'll be many of them), I want the longer string in textbox2 to replace the contained Textbox1 element at its position in textbox1. The problem I have is NOTHING HAPPENS when I click my button with the code below. Help me please, I am a newbie.

    IILLUSTRATION:
    If Textbox1 initially contains
    CLB035 8/30/2014 11:23
    CLB041 8/30/2014 16:03
    CLB042 8/24/2014 13:52
    CLB043 8/30/2014 11:23


    Textbox2 contains
    CLB041 8/30/2014 16:03 8/30/2014 16:16
    CLB042 8/24/2014 13:52 8/24/2014 18:49


    EXPECTED RESULT
    Textbox1 should now contain
    CLB035 8/30/2014 11:23
    CLB041 8/30/2014 16:03 8/30/2014 16:16
    CLB042 8/24/2014 13:52 8/24/2014 18:49
    CLB043 8/30/2014 11:23



    Here's my code

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim aryreport() As String = TextBox1.Text.Split(vbCrLf)
    Dim aryserver() As String = TextBox2.Text.Split(vbCrLf)
    For i As Integer = 0 To UBound(aryreport)
    Dim reporter As String = aryreport(i)
    For j As Integer = 0 To UBound(aryserver)
    Dim alldata As String = aryserver(j)
    If alldata.Contains(reporter) Then
    reporter = reporter.Replace(reporter, alldata)
    End If
    Next
    Next
    End Sub
    End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Replacing Elements of An Array with Elements from Another Array (VB.NET)

    Ok, it's a fair start. You have code, and it is running without errors, it just isn't doing what you expected it to. The next step is to put a breakpoint somewhere and step through the code to see what is happening. In this case, a reasonable place to put the breakpoint may appear to be the first For statement, but that probably isn't the best place, because nothing much happens at that point. I'd put a breakpoint on the inner For statement.

    When execution stops on the breakpoint, you can look at aryreport and aryserver to see that they have what you expect. I think they probably will, but it's worth a look. You can then look at whether reporter holds what you expect, but if aryreport is correct, then reporter will also be correct.

    After that, step forwards through the code a couple lines to the If statement. This is the place where interesting things start happening, as long as the two arrays hold what you expect. You could keep stepping forwards until the If statement becomes True, but that might require MANY iterations of the inner loop, and possibly even several iterations of the outer loop, so it could be phenomenally boring. A better approach would be to remove the breakpoint on the For loop and add breakpoints both on the reporter = reporter.Replace(reporter, alldata) line as well as on the End Sub. The latter breakpoint is to catch the case where the If statement is NEVER true. With those two breakpoints in place, just run until one of the breakpoints is hit. If the second one is reached without the one where reporter is assigned, you'd know that the If statement was never true. If you think that it should have been true at least once or twice, then there is something wrong with the comparison between the strings.

    More likely, you will reach that inner breakpoint, because the If statement will probably work as you expect. At that point, you would know that the code is pretty much working, except....I'm tempted to leave it there. After all, if you get that far, you will probably see what the problem is. However, since you were asking for an answer, I'll point out that you change what is in reporter, but that's ALL you do. That doesn't impact what is in the aryreport in any way, and even if it did, what is in the aryreport doesn't impact what is shown in the textbox in any way, so your code could be working just fine as far as it goes. The problem may be simply that you are making the change and then doing nothing with it, so the change is immediately discarded.

    One point to note is that there is no reason to use .Replace in that line. All you really need for that line is report = alldata, but that wouldn't solve the ultimate problem. A better solution would be:

    aryreport(i) = alldata

    That would at least get the data into the array correctly. However, you'd still have another step to put it back into the textbox.
    My usual boring signature: Nothing

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replacing Elements of An Array with Elements from Another Array (VB.NET)

    try this:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim lines() As String = TextBox1.Lines
            For x As Integer = 0 To lines.GetUpperBound(0)
                Dim i As Integer = Array.FindIndex(TextBox2.Lines, Function(l) l.StartsWith(lines(x)))
                If i > -1 Then lines(x) = TextBox2.Lines(i)
            Next
            TextBox1.Lines = lines
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    7

    Re: Replacing Elements of An Array with Elements from Another Array (VB.NET)

    Quote Originally Posted by .paul. View Post
    try this:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim lines() As String = TextBox1.Lines
            For x As Integer = 0 To lines.GetUpperBound(0)
                Dim i As Integer = Array.FindIndex(TextBox2.Lines, Function(l) l.StartsWith(lines(x)))
                If i > -1 Then lines(x) = TextBox2.Lines(i)
            Next
            TextBox1.Lines = lines
        End Sub
    
    End Class
    Thanks a lot, it worked! You are the best!! After you, it's you again!!!

  5. #5

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    7

    Re: Replacing Elements of An Array with Elements from Another Array (VB.NET)

    Quote Originally Posted by Shaggy Hiker View Post
    Ok, it's a fair start. You have code, and it is running without errors, it just isn't doing what you expected it to. The next step is to put a breakpoint somewhere and step through the code to see what is happening....
    Thanks for the painstaking effort. I am super grateful!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Replacing Elements of An Array with Elements from Another Array (VB.NET)

    Quote Originally Posted by 1supremo View Post
    Thanks for the painstaking effort. I am super grateful!
    Shaggy gave you good advice. I would use that strategy in your situation...

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