CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    1

    C sharp Program help

    Need some help with my c sharp program I have a few errors that I could really use some help on. One error is that I made a checkId method to check and see if the IDS matched. If the two IDs match then it sends a message saying it cannot add the student because the ids are the same. The problem I am having is that i cannot figure out where to call this method. And the other problem i am having is that when I click the next student button it goes until the end of the array and not the last person in the list. And when I click the last student button it goes just goes straight to the end of the array and not to the last student in the array. Any help would be appreciated Thanks
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C sharp Program help

    I am a little confused about your code. In the main form I see you make a List<Student> and in PersistantData you create the same List<Student> but they are modified at different instances in your application.

    Your (Add/Remove) buttons remove values from the list in PersistantData but from what I can tell leave the list in your main form untouched. Since it appears untouched you do not notice the differences until you save -> close -> open application so it loads new values.

    First, Prev, Next button seem to work as they should from what I tested. You could create a check on the next and prev buttons to make sure there is another value to move on to if not disable them to prevent getting an out of bounds exception.

    The Last button works as it should as well but instead of a loop you could also find the last used index in the list without looping with something like this:
    Code:
    int index = studentList.Count - 1;
    
    DisplayStudent(studentList[index]);
    With your CheckID method you would call it before adding the new student to prevent adding it into the list when it is already in use.

    I am not sure about what professionals do but I would recommend sticking to modifying a single list and getting the values from that list until you are ready to store them in a file.

    For example:
    Code:
    PersistantData data = new PersistantData();
    List<Student> studentList = new List<Student>(data.RetreiveStudents());
    int index = 0;
    
    private void btnRemove_Click(object sender, EventArgs e)
    {
        // Remove the student from the list 
        studentList.RemoveAt(index);
    
        // Remove 1 from index to match the student removed and display another student's info
        index--;
        DisplayStudent(studentList[index]);
    }
    
    private void btnLast_Click(object sender, EventArgs e)
    {
        // Set index to last value with information in the list
        index = studentList.Count - 1;
    
        // Display the students data
        DisplayStudent(studentList[index]);
    
        // Disable/Enable buttons
        btnFirst.Enabled = true;
        btnPrev.Enabled = true;
        btnNext.Enabled = false;
        btnLast.Enabled = false;
    }
    
    private void btnSave_Click(object sender, EventArgs e)
    {
        // Save the list in a file
        data.Save(studentList);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured