1

I got this database witch would display the grade of a student based on what I would write in the code. For example:

id = "Robert"
classType = "mathGrades"

And here is my code

public string grades;

public string loadQuery(string id, string classType)
{
    students = jsonService.GetData(); // My Json reader gets data from my Json file
    string Class = "student." + classType; //I combine the scripts

    foreach (Student student in students)
    {
        if (student.name == id)//Check if we have a student by this name, if we do then get the grades for the class defined in "classType"  
        {
            grades = string.Join(", ", Class);//This is the part that I understand why it doesn't work, but I don't have an idea on how to replace it
        }
    }
    return grades;
}

This is the Student class:

public class Student
{
    public string name { get; set; }

    public List<int> mathGrades { get; set; }

    public override string ToString() => JsonSerializer.Serialize(this);
}

And this is the json file containing the data

 [
      {
        "name": "Test1",
        "mathGrades": [ 1, 2, 3 ]
      },
    
      {
        "name": "Test2",
        "mathGrades": [ 1, 2, 3 ]
      }
]

I would want my output to be something like "1, 2, 3" but I just get student.mathGrades. Which I understand why, I just don't know how to fix it. Thanks in advance !

2 Answers2

1

You should change

 grades = string.Join(", ", Class);

to

grades = string.Join(", ", student.mathGrades);

The above answer explained your mistake. String.Join expects array of values. In your example

string.Join(", ", "student.mathGrades");

returns student.mathGrades

Max
  • 1,070
  • 3
  • 13
  • 19
  • I know, but I am searching for an alternative way. I want to avoid hard-coding 13 functions for 13 different classes, and just call the function with the name and the class that I want the grade from. For example loadQuery("Robert", "mathGrades"). And it would pull up the math grades for the student named Robert. – CallMeTrebor Jul 14 '20 at 20:22
  • @CallMeTrebor, I don't get, where is hardcode inside your function? Why do you need 13 of them? – Max Jul 14 '20 at 20:29
  • I would like to create a modular system where you could easily pull up grades, and in total I would have 13 classes, for now I am just experimenting with one. For example: English, German, Geography and so on. And I would like to experiment with creating a system where I don't have to write a function for every single class. So if I wanted the German grades I wouldn't need to make a function for getting the German onesm, I would just tell the 'loadQuery' function to pull it up, instead of making a 'loadQueryGerman' function. – CallMeTrebor Jul 14 '20 at 20:36
  • 1
    I see, that wasn't described in your question. No need to create 13 methods like loadQueryGerman, instead you need to change Student class structure instead of List mathGrades, you should have a List subjects(each subject has name and grades). And search for a value by nested loop. – Max Jul 14 '20 at 20:46
0

You are creating a string named Class and setting it equal to "student.mathGrades".

In your loop, you are effectively saying grades = string.Join(", ", "student.mathGrades")

Since "student.mathGrades" is just a single string, there is no joining and it's just returned.

If you want to have grades return a list of grades you need to pull that list from somewhere. It would help to know what your student object looks like.

Todd Skelton
  • 6,839
  • 3
  • 36
  • 48