Reputation: 131
I hope I am not breaking any rules here. I have a question about another post, but I am not a big user on stackoverflow, so my reputation is too low to add a comment to questions or answers that are not my own.
On this question: How to compare two rich text box contents and highlight the characters that are changed?
TaW provided some sample C# code and we have made use of that in a Visual Studio project. But, we discovered a problem and don't know how to fix it.
If RTB1 contains the text "My name is David" and RTB2 contains the text "My name is", then after the comparison is run there are two diffs in the diffs collection and somehow, when the rich text boxes are rewritten to show the differences, RTB1 is an exact match of RTB2 and nothing is highlighted. Maybe this is the expected behavior and we just are not realizing that, but we were hoping that the text " David" in RTB1 would be highlighted.
If the text in RTB2 is "My name is " (two added spaces at the end of the line), then we get the expected behavior.
I should have mentioned that we wrote a VB.NET equivalent of TaW's C# code and just noticed a difference. I have noted the difference in the comments.
If I was up to 50 reputation, I would have also added in my comment that we are very thankful to TaW for sharing his example, as well as the creator of DiffMatchPatch.
Upvotes: 0
Views: 1274
Reputation: 131
I think we figured out the problem. In our project we are using vb.net and we are fairly certain we translated correctly from C# to VB. However in the collectChunks function in C#, you are comparing RTB and RTB2 as objects, not the text property within the objects. so for instance, when you compare RTB and RTB2, even though the text in the two text boxes being compared is equal, your code is comparing the objects, and all their other associated properties, including the text box positions. Therefore, the first == is always false.
In VB, we are not allowed to do an object comparison. i.e. We are not allowed use RTB = RTB2, we must use RTB.Text = RTB2.Text in the if statement. (There is a way to compare the RTB objects in VB, but I am guessing what really needs to be compared is the text property in the RTB and RTB2 objects). If this is the case, is it possible that the results you got were based on an assumption that the text in the text boxes were being compared? And perhaps that assumption led you to code the way you decided to stay in or jump out of the for loop?
Upvotes: 0