C_Louis
C_Louis

Reputation: 13

OOP Array filtering using a string?

So I'm pretty new to C# OOP and I'm just getting the hang of this style of programming at college and I've come across arrays. Now, I understand how to create them and their general use, but I don't understand how I would filter them using a string.

For a little back story this is part of what I have been asked to do;

In part of my project I needed to create three arrays, set out the topic information and then output it to the user with a display method. I was also required to use the _findExamIndex (Searches the exams array for results based on an exam id given) to list the exams, to then use the _findStudentIndex (Searches a list of students for a student) to be able to select a individual students results.

The problem I am having issues is with filtering the results of the _exams array with the string _topicName, I'm unsure as to how to go about doing that as I am new to using arrays as a way of storing data and would greatly appreciate a little help, many thanks.

(Method code)

public void viewTopicsResults(string _topicName,List<Results> _results, List<Student> _students, Exam[] _exams, DisplayClass.Display _theDisplay)
{
        int[] columnWidths = { 3, 20 };
        char[] charactersInTheSpaces = { ' ', ' ' };
        string[] topicInfo = new string[6];

        topicInfo[0] = "Student";
        topicInfo[1] = "Subject";
        topicInfo[2] = "Topic";
        topicInfo[3] = "Style";
        topicInfo[4] = "Date";
        topicInfo[5] = "Score";


        //Ordering the table by topic.      
        _theDisplay.writeLineWithTab(topicInfo, columnWidths, charactersInTheSpaces);


        foreach(Results result in _results)
        {
            int examID = _theDisplay.userInputAsInteger("Select a result");
            _findExamIndex(examID, _exams);
             int studentID = _theDisplay.userInputAsInteger("Please select a users result");
            _findStudentIndex(studentID, _students);

             //filter on the _exams' Topic == '_topicName'

        }
}

(This method will be called by the main method)

Upvotes: 0

Views: 170

Answers (2)

C_Louis
C_Louis

Reputation: 13

Many thanks, after much deliberation, changing and adapting code i have come up with this solution;

char[] padding = { ' ', ' ', ' ', ' ', ' ', ' ' };
        int[] widths = { 20, 9, 26, 14, 11, 4 };
        string[] topicInfo = { "Student", "Subject", "Topic", "Style", "Date", "Score" };

        _theDisplay.clearScreen();
        _theDisplay.writeLineWithTab(topicInfo, widths, padding);

        for (int i = 0; i < _results.Count; i++)
        {
            int examIndex = _findExamIndex(_results[i].ExamRegisterNos, _exams);
            int studentIndex = _findStudentIndex(_results[i].StudentIDNumber, _students);

            if (_exams[examIndex].Topic == _topicName)
            {
                topicInfo[0] = Convert.ToString(_students[studentIndex].studentName());
                topicInfo[1] = _exams[examIndex].Subject;
                topicInfo[2] = _exams[examIndex].Topic;
                topicInfo[3] = _exams[examIndex].StyleOfExam;
                topicInfo[4] = Convert.ToString(_results[i].TakenOn.ToShortDateString());
                topicInfo[5] = Convert.ToString(_results[i].IntGradeOutOf100 + "%");

                _theDisplay.writeLineWithTab(topicInfo, widths, padding);
            }
        }

Thank you for your help, it was much appriciated.

Upvotes: 0

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

just do somtehing like this

    foreach(Results result in _results)
    {
        int examID = _theDisplay.userInputAsInteger("Select a result");
        _findExamIndex(examID, _exams);
         int studentID = _theDisplay.userInputAsInteger("Please select a users result");
        _findStudentIndex(studentID, _students);

         //filter on the _exams' Topic == '_topicName'
         exam[]  exams  = FindExamByTopic("youtopic",_exams)

    }


  private Exam[] FindExamByTopic(string topic, Exam[] _exams)
    {
         return _exams.Where(ex => ex.Topic == topic).ToArray();  
    }


  //here without using  linq 

     private Exam[] FindExamByTopicSimple(string topic, Exam[] _exams)
    {
        List<Exam>  exams = new List<Exam>();

        for (int i = 0; i < _exams.Length; i++)
        {
            if (_exams[i].Topic == topic)
            {
                exams.Add(_exams[i]); 
            }
        }
        return exams.ToArray();   
    }

Hope this help

Upvotes: 2

Related Questions