0

I require a way to store the smallest time to replace any existing, but currently what I have tried [below] doesn't work and may sometimes say that 2:38.4 is smaller than 2:20.1.

In the text file

88:88:8

In the form 3 text boxes

timerMin
timerSec
timerMil

Writing into a correct path.

                using (TextReader reader = File.OpenText(pathPlayer + player[id].name + "\\time.txt"))
                {
                    string z = reader.ReadLine();
                    string[] zsplit = z.Split(':');
                    reader.Close();
                    fileMin = Convert.ToInt32(timerMinute.Text);
                    recMin = Convert.ToInt32(zsplit[0]);
                    if (fileMin < recMin)
                    {
                        File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                        newPersonalRecord = true;
                    }
                    else
                    {
                        fileSec = Convert.ToInt32(timerSecond.Text);
                        recSec = Convert.ToInt32(zsplit[1]);
                        if (fileSec < recSec)
                        {
                            File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                            newPersonalRecord = true;
                        }
                        else
                        {
                            fileMil = Convert.ToInt32(timerMili.Text);
                            recMil = Convert.ToInt32(zsplit[1]);
                            if (fileMil < recMil)
                            {
                                File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                                newPersonalRecord = true;
                            }
                            else
                            {

                            }
                        }
                    }

                }

I have been working on this for quite a while and I cannot see where I have gone wrong, and help would be brilliant.

Thanks

3 Answers3

2

you are comparing the text boxes when you should be comparing the TimeSpans

if the strings in the file do not exceeds the time of a day (up to "23:59:59") then you can use the strings to create TimeSpans by doing TimeSpan.Parse("18:44:08"); and compare them like

                fileMin = new TimeSpan(0, 0, int.Parse(timerMin), int.Parse(timerSec), int.Parse(timerMil));
                recTimeSpan = TimeSpan.Parse(z);

                if(fileMin > recTimeSpan)
                {// Your code}
                else
                {// Your code}

you can always do

                recTimeSpan = new TimeSpan(0, 0, int.Parse(zsplit[0]), int.Parse(zsplit[1]), int.Parse(zsplit[2]));
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • What timespans? I believe the OP is comparing an integer from a textbox to an integer from a file (for each part) - which should be fine. Also, the time is not hours, minutes, seconds. It is minutes, seconds, milliseconds.. are you reading the same question as me? – musefan Aug 12 '13 at 13:14
  • @musefan he says the file contains string like: "88:88:8" – No Idea For Name Aug 12 '13 at 13:16
  • Yeah, and is 88 a valid minute or second? no, so that value should be assumed to indicate an undetermined format, where `8` represents a digit. The OP then shows clearly that his 3 components are minutes, seconds and milliseconds due to his variable naming – musefan Aug 12 '13 at 13:18
1

This is more of a design quip but I would strongly suggest you use the TimeSpan class for something like this rather than integers; it would make your code a lot easier to both write and read. You could construct a new TimeSpan off of the time data you're retrieving and then just use one comparison operator to determine whether it's greater, less than or equal to the existing record, rather than one for seconds and milliseconds without minutes.

DateTime could also work if you use it to track start and end times (you can find the difference between DateTimes as a TimeSpan very easily by just using the subtraction operator)

For example:

DateTime StartTime
DateTime EndTime 
TimeSpan difference = EndTime = StartTime 
UpQuark
  • 791
  • 1
  • 11
  • 35
0

Your code has a lot of repetition that you should aim to eliminate. You can also use TimeSpan to store the values and compare them.

Try something like this:

string filePath = pathPlayer + player[id].name + "\\time.txt";
string z = null;
using (TextReader reader = File.OpenText(filePath))
{
    z = reader.ReadLine();
}
string[] zsplit = z.Split(':');

//Create a timespan from the values read from the .txt file
TimeSpan fileTime = new TimeSpan(0, 0, int.parse(zsplit[0]), int.parse(zsplit[1]), int.parse(zsplit[2]));
//Create a timespan from the values stored in the Textbox controls
TimeSpan inputTime = new TimeSpan(0, 0, int.parse(timerMinute.Text), int.parse(timerSecond.Text), int.parse(timerMili.Text));

//Check if the new TextBox time is faster than the time stored in the file
if(inputTime < fileTime)
{
    //new faster time, so update the file
    File.WriteAllText(filePath, inputTime.ToString("mm:ss:f"));
    newPersonalRecord = true;
}

A few things to note:

  • There is no validation on the input, so invalid data will crash the app (you should add validation, see: int.TryParse). You will also want to validate z is not null, and zsplit has a Length of atleast 3
  • Your naming of fileMin and recMin didn't match up with your data source, I renamed them to fileTime and inputTime to be more clear
  • This example checks if inputTime is less than fileTime, if you want it the other way then just switch them around in the if statement
  • The use of TimeSpan in this example assumes that the minute component can never be greater than 59
musefan
  • 47,875
  • 21
  • 135
  • 185