Results 1 to 6 of 6

Thread: Same code, different results?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Same code, different results?

    Hello! So, I am having this issue with a snippet of code. I copy/pasted this code from one project to another, made no changes and the results are different. Here is the code:
    Code:
     Dim fName As String = ""
            OpenFileDialog1.InitialDirectory = "c:\"
            OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
            OpenFileDialog1.FilterIndex = 2
            OpenFileDialog1.RestoreDirectory = True
            Dim result As DialogResult = OpenFileDialog1.ShowDialog
            ' OpenFileDialog1.ShowDialog()
            If (result = DialogResult.Cancel) Then
    
            ElseIf (result = DialogResult.OK) Then
                DataGridView1.Rows.Clear()
                DataGridView1.Refresh()
                fName = OpenFileDialog1.FileName
                Dim TextLine As String = ""
                If IO.File.Exists(fName) Then
                    Dim Contents = IO.File.ReadAllLines(fName).ToList
    
                    Contents.RemoveAt(0)
                    Contents.RemoveAt(Contents.Count - 1)
                    'Contents.RemoveAt(Contents.Count - 1)
    
                    For x As Integer = 0 To Contents.Count - 1
                        Contents(x) = Contents(x).Replace(",,,", ",").Replace(",,", ",")
                        Contents(x) = Contents(x).Substring(0, Contents(x).Count - 1)
                        Dim RowItem = Contents(x).Split(","c)
                        DataGridView1.Rows.Add(RowItem)
                    Next
                End If
            End If
    This is a complete copy/paste from other code I have worked on that 100% successfully worked. The issue is, here is the file:Name:  issue.PNG
Views: 255
Size:  8.2 KB
    the line of Deadpool hi, that will not show but in the other program I have created, it will show all of the rows with no issue. Any ideas? The saving code is 100% copy/paste too and still, 100% successful with the other program.

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

    Re: Same code, different results?

    Have you debugged the code? If not, do that now. Step through it line by line in both projects and see exactly where and how it behaves differently. You'll rarely solve any but the simplest problems just by reading the code. You need to run it and watch it in action. So many things become obvious when you do.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Same code, different results?

    I have debugged the code, that is how I am aware of this issue. I will go through both identical pieces of code though and try to find why they are behaving differently. The thing I am not understanding is that if it is the same exact code, no changes, why it would be behaving differently. I am seeing no ways it would behave differently

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Same code, different results?

    Quote Originally Posted by Dragnorian View Post
    I have debugged the code, that is how I am aware of this issue.
    Have you actually debugged the code or have you just run it in the debugger? If you haven't set a breakpoint and stepped through the code then you haven't actually debugged.
    Quote Originally Posted by Dragnorian View Post
    I am seeing no ways it would behave differently
    Which is exactly why reading code is usually insufficient and watching it in action is required. Assuming no corruption, the same code will behave the same way given the same data. If your code behaves differently and there's no corruption then the data must be different somewhere but you can't always see that just by reading the code because you don't/can't follow the path of execution and know what data will be used at every step. If you do debug and the code does behave exactly the same then at least you've confirmed that the issue is not the code and you can stop wasting time on that and look for issues elsewhere.

  5. #5
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Same code, different results?

    I have had this happen before, well not with this exact code or anything with excel, I have even had code to "break" after overuse.

    The thing about it is, it could be that the code you wrote, isn't effecient (thread.sleep for example is one we talk about a lot on here not being best way) or isn't being compiled correctly.

    Code can behave differently with different interferences. Can't say for certain but usually its a interference, or a bug in the IDE and or VB.NET compiler (Or VSHost whatever compiles the code in VS)
    Disclaimer: When code is given for example - it is merely a example.




    Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Same code, different results?

    I've seen compiler bugs, too, but the only way I've ever found them, and fixed them, is by doing what JMC said. There's no getting around setting a breakpoint and stepping through the code. If the problem just goes away without you changing anything, then that was a glitch in the compiler generated code. Those are mighty rare, though, and it is more likely that you'll find that the inputs are not the same as you thought they were. I dealt with that kind of case just yesterday. It happens, and only stepping through the code and seeing how things really are will show it.
    My usual boring signature: Nothing

Tags for this Thread

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