Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, at least three ways.</p> <ol> <li>Make grades and teacher methods of the Student class: </li> </ol> <hr> <pre><code>public class Student { int grade; //0 - 100 as opposed to the characters A, B, C, etc. string teacher; //For the teacher of the class the student is in. public void SetGrades() { grade = 100; } public void SetTeacher() { teacher = "John Smith"; } } class Program { public static void Main() { Student Jim; Jim = new Student(); Jim.SetTeacher(); Jim.SetGrades(); } } </code></pre> <p>or 2. Pass the student to the methods:</p> <hr> <pre><code>class Program { public static void Main() { Student Jim; Jim = new Student(); grades(Jim); teacher(Jim); } static void grades(Student student) { student.grade = 100; } static void teacher(Student student) { student.teacher = "John Smith"; } } </code></pre> <p>or 3. Use Jim as a variable in the Program class, so you don't actually have to pass it to anything</p> <hr> <pre><code>class Program { static Student Jim; public static void Main() { Jim = new Student(); grades(); teacher(); } static void grades() { Jim.grade = 100; } static void teacher() { Jim.teacher = "John Smith"; } } </code></pre> <hr> <p>Note that in all examples I made the Student class have an uppercase S in its name, to distinguish it from the parameter and any local variables you might have.</p> <p>It's important to know the differences.</p> <p>In the first solution, you ask the object itself to set the grades and the teacher. If these are operations you do a lot, they probably belong in the class itself, instead of being operations outside.</p> <p>In the second solution, you pass around the student. This allows you to easily work with multiple students, since the code isn't hardwired to work with a single object.</p> <p>In the third solution, you don't have to pass anything around, but your code is now locked to only working with one student at a time, in the Jim field.</p> <p>Personally I would work out a variation of the first solution, only a bit more flexible in terms of what grade it would set, and which teacher. Probably using properties (which you should also look more into.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload