Question A strange issue with not reading a line before or after another line

Lawh

Member
Joined
Jan 17, 2024
Messages
6
Programming Experience
Beginner
Here is my code:

C#:
//Version that reads badGame = -1,
//fileData = new string[lengthArray];
//fileData = fileDataFixed;
FileWriteLines(pathGames, fileDataFixed);
fileData = FileReadLines(pathGames);
badGame = -1;

//Versions that do not read the line
//1
badGame = -1;
//fileData = new string[lengthArray];
fileData = fileDataFixed;
//FileWriteLines(pathGames, fileDataFixed);
//fileData = FileReadLines(pathGames);

//2
//fileData = new string[lengthArray];
fileData = fileDataFixed;
//FileWriteLines(pathGames, fileDataFixed);
//fileData = FileReadLines(pathGames);
badGame = -1;

//3
fileData = new string[lengthArray];
fileData = fileDataFixed;
//FileWriteLines(pathGames, fileDataFixed);
//fileData = FileReadLines(pathGames);
badGame = -1;

//4
badGame = -1;
fileData = new string[lengthArray];
fileData = fileDataFixed;
//FileWriteLines(pathGames, fileDataFixed);
//fileData = FileReadLines(pathGames);

Inside the methods to read and write I have error messages set up in case something goes wrong, but other than that they are exactly the same as File.Read/Write methods.

I need the badGame = -1; to run, and I can get it to run with the code that I set. However, since I have spent a while on this issue and only finding a solution by thinking of trying something different for no other reason as to understand why a line of code will not read and get's hopped over, I would like to know why so that I can avoid things like this in the future. It's bad enough to look for your mistakes and fix them. It gets impossible to carry on after a while when you will never know if an error is yours or if the software happens to have a kink. Maybe that kink happens to someone else after they use it and no me. A random line of code skipped for no real logical reason is not great.

So can someone enlighten me why first writing and then reading the same data will run the next line of code, but if I use array = array2, the line after it will not run. Thanks!

P.S If I seem frustrated it's because I want to be able to trust a language in simple things like this. If I can't, and I spend hours looking for an error that I didn't even make, it's going to affect my state of mind and my workflow. I could have probably published what I have made by now not to mention going to sleep or doing other things if I wasn't trying to figure this out.

How common is this with programming in general. Do things often not work sometimes for no clearly apparent reason?

EDIT: When I debug it, badGame shows up as -1 everywhere as a console write. However, the code doesn't seem to care and keeps going on like it's not -1. At some point it showed up as 0, which now didn't happen as I kept writign it to console at every possible turn.

C#:
public static string[] FileReadLines(string pathFile)
{
    if (File.Exists(pathFile))
    {
        string[] txt = File.ReadAllLines(pathFile);
        return txt;
    }
    else
    {
        Message("Read lines failed: File doesn't exist.");
        string[] txt = { "error" };
        Timer();
        return txt;
    }
}

public static void FileWriteLines(string pathFile, string[] txtArray)
{
    if (File.Exists(pathFile))
    {
        File.WriteAllLines(pathFile, txtArray);
    }
    else
    {
        Message("Creating new file..");
        File.WriteAllLines(pathFile, txtArray);
    }
 
Last edited:
Have you actually debugged using breakpoints and stepping through the code line by line? If you're talking about writing to the console, it sounds like you haven't. Confirm that your build is successful so you're not running an old build and ensure that you're running a Debug build and that optimisations are not enabled for that build configuration. You should then be able to step through the code line by line in the debugger and see exactly what course execution takes and evaluate any relevant variables and other expressions at any time using the Autos, Locals, Watch and Immediate windows.
 
Unrelated to your issue, you should familiarise yourself with the DRY principle: Don't Repeat Yourself. You should almost always avoid writing the same code multiple times. Large blocks of such code should be extracted into a method and that method called multiple times. On a smaller scale, you have two cases of the same code being in both an if and an else block. This:
C#:
public static string[] FileReadLines(string pathFile)
{
    if (File.Exists(pathFile))
    {
        string[] txt = File.ReadAllLines(pathFile);
        return txt;
    }
    else
    {
        Message("Read lines failed: File doesn't exist.");
        string[] txt = { "error" };
        Timer();
        return txt;
    }
}
could be written like this to avoid that repeated line:
C#:
public static string[] FileReadLines(string pathFile)
{
    string[] txt;
   
    if (File.Exists(pathFile))
    {
        txt = File.ReadAllLines(pathFile);
    }
    else
    {
        Message("Read lines failed: File doesn't exist.");
        txt = { "error" };
        Timer();
    }
   
    return txt;
}
while this:
C#:
public static void FileWriteLines(string pathFile, string[] txtArray)
{
    if (File.Exists(pathFile))
    {
        File.WriteAllLines(pathFile, txtArray);
    }
    else
    {
        Message("Creating new file..");
        File.WriteAllLines(pathFile, txtArray);
    }
}
could be written like this:
C#:
public static void FileWriteLines(string pathFile, string[] txtArray)
{
    if (!File.Exists(pathFile))
    {
        Message("Creating new file..");
    }
   
    File.WriteAllLines(pathFile, txtArray);
}
You might think that, in the first case at least, this doesn't help because the code gets no shorter, but shorter code is not the point. It's a benefit in many - even most - cases but the point is that, if you need to change that code for any reason, you only have to do so in one place. That makes changes easier but also safer, because you can't accidentally miss one of the places in which the code is repeated.
 
I've debugged it and used breakpoints, so I'm not sure where you heard that.

I also would love to stop and set everything up nicely, but the thing is, I am stuck here for hours trying to build a system from scratch, getting an error that makes no sense on very little sleep. The system runs and I could sit down and have a cup of tea and go over all the little optimizations, but I feel that this is more important right now.

Why is a line of code not being realized, when it is clearly read and everywhere else it is used as i have intended, except where it matters?
 
I've debugged it and used breakpoints, so I'm not sure where you heard that.

If you're using breakpoints and stepping through the code then you don't need to use Console.WriteLine to see the value of a variable. The fact that you were suggested to me that you weren't really using the debugging tools to their fullest.

I would put a breakpoint on the line(s) you want to see executed and then run it in the debugger and see if they are hit. I'd be inclined to put breakpoints before and/or after as well, because that will indicate whether that section of code is being executed but that line is not. If your build configuration does not have optimisations enabled (if it does then there may not be a 1:1 correspondence between what you see and what gets executed) and you are genuinely seeing lines of code being skipped, I can only conclude that your IDE is corrupt and you should perform a repair from the VS installer.
 
+1

Better than just setting breakpints, stepping through the code line by line will show you exactly what the execution path being taken by the program.

I will also reiterate: Make sure you are debugging a current build, and that that you built with optimizations turned off.

Personally, I have my suspicions about your Message() and Timer() methods. If they are not implemented correctly, specially if old VB6 code was the reference for that implementation, then you could possibly have unexpectedly re-entrant code. Debugging such code can be very hard to debug and fix.
 
Last edited:

Latest posts

Back
Top Bottom