Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
Hello,

I am writing code to correct the error in a digital scanner that sometimes reads 2 as ">".

If the scanner includes this wrong character then the code below will execute:


if ((string)row.Cells[1].Value != txtScannedIn.Text.Trim())
                {
                    string editedtext;
                    editedtext = ScanFix(txtScannedIn.Text, (string)row.Cells[1].Value);
                    txtScannedIn.Text = editedtext;
                    
                }


The method that it uses to fix the code is this:

private string ScanFix(string NotFound, string rightpart)
        {
            string replacer;
            if (NotFound.Contains('>'))
            {
                replacer = NotFound.Replace('>', '2');
                if (replacer == rightpart)
                {
                    MessageBox.Show(replacer + " " + rightpart);
                    return replacer;
                }
                else return NotFound;
            }
            else
            {
                return NotFound;
            }
            
        }


These two code segments work exactly as they should when executed like this, however when I try to make the first block of code display a MessageBox with the updated "editedtext" string, the messagebox displays the original incorrect scan with the ">" in it.

if ((string)row.Cells[1].Value != txtScannedIn.Text.Trim())
                {
                    string editedtext;
                    editedtext = ScanFix(txtScannedIn.Text, (string)row.Cells[1].Value);
                    MessageBox.Show(editedtext); //this displays the originally wrong scan with the ">" instead of the corrected 2. Without this single MessageBox line the code runs properly and the > is replaced. 

txtScannedIn.Text = editedtext;
                    
                }


What I have tried:

I have tried creating a separate variable to set the editedtext string equal to and then displaying that (if it works when I declare the txtScannedIn.Text to it why would it not allow that variable to be used?), and I have inserted MessageBox commands in other areas of the code to determine if they run (from my understanding including this messagebox OUTSIDE of the ScanFix method changes it to return the original NotFound value).

One thing that did allow the messagebox to display the correct scan/ the editedtext string to be updated the right way was if the message box is

MessageBox.Show(editedtext = ScanFix(txtScannedIn.Text, (string)row.Cells[1].Value));

I have never used this forum but I cannot find any documented case of this problem and nothing I have tried is allowing the Messagebox to simply display the return of the fixed scan the right way. If anyone sees this and knows how to fix this, I will greatly appreciate your help.

Thank you
Posted
Updated 19-Jul-19 5:39am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Put a breakpoint on the line
editedtext = ScanFix(txtScannedIn.Text, (string)row.Cells[1].Value);
then press F5 and wait for the breakpoint to be reached. Once in debug session, start debugging line by line to figure out why the editedtext variable does not get the value you thought it would.

Debugging is a highly useful, not optional skill for a developper. I even find it quite distractive. You shoud give it a try :)
   
Comments
andrew898701 19-Jul-19 12:00pm
   
Hello,

Thank you for the response.

I tried using a breakpoint to read what the "edittext" variable was defined as within that line and the lines after it. In scenarios where the code is working or not working the "edittext" variable is still defined as the wrong scan input within and after that line. Any idea why it might be reading the wrong string but passing along the right one//tips on how to use the 'breakpoint' feature more effectively? (I am new to coding and have been referencing online sources to figure this out but any help is appreciated).

Thanks
andrew898701 19-Jul-19 12:09pm
   
Hello,

I was able to figure it out- turns out it was working and the messagebox was from the code cycling through the rest of the datagridview- thanks for the help!
phil.o 19-Jul-19 12:11pm
   
You're welcome.
phil.o 19-Jul-19 12:10pm
   
The problem seems to be in the ScanFix method then; you should debug this method and see how it produces wrong results.
Following the advice in my solution, once the breakpoint is reached, press F11 and you will be able to debug the code inside the faulty method.
Here's an introduction to debugging:
Tutorial: Learn to debug C# code using Visual Studio[^]
You can find many other sources about that subject using your favorite search engine.
BillWoodruff 20-Jul-19 5:02am
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100