A.11. Chapter 13

A.11.1. Exercise 1 Solution

There's probably a bazillion different ways to accomplish this task, of which the following involves simple changes to the ReadAndShowRecord() method:

private int ReadAndShowRecord()
{
    int flag;

    try
    {
        myData.Open(myData.FileName);
        flag = myData.ReadOneRecord(currentRecord - 1);
        if (myData.Status == 0)
        {
            ClearTextboxes();
            txtRecord.Text = currentRecord.ToString() + " Not Active";
        }
        else
        {
            if (flag == 1)
            {
                ShowOneRecord();
                txtRecord.Text = currentRecord.ToString();

            }
            else
            {
                MessageBox.Show("Record not available.", "Error
                                 Read");
                flag = 0;
            }
        }
     }
     catch
     {
         flag = 0;
     }

     myData.Close();
     return flag;
}

This solution displays the record position (currentRecord) in the txtRecord object, but appends a Not Active message after clearing out all the textboxes. Presenting the information this way doesn't leave the user in the dark when an inactive record is read.

You could also put Read Active and Read All radio buttons on the form to give the user more control over reading the data.

A.11.2. Exercise 2 Solution

Obviously you can, or I wouldn't have asked the question. In the getRecordCount() method from Listing 13-6, delete the following line:

records = myFile.Seek(0, SeekOrigin.End); // Position file pointer

Replace it with this:

FileInfo myInfo = new FileInfo(fileName);
records = myInfo.Length;

This exercise simply confirms that there are almost always multiple ways to accomplish a given task. Part of the journey to becoming an experienced programmer ...

Get Beginning C# 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.