c# - Using objects from one class in another class -
c# - Using objects from one class in another class -
i have 2 classes teacher
, student
. trying teacher utilize class functions in students. problem having need number of pupil object beingness used in teacher
random. thought figured out in constructor, had declare new pupil object in every function of teacher utilize student. creates new pupil object me no good. here code
class teacher { private bool absence; private bool level; private static int uniqueid = 0; private arraylist arraylist = new arraylist(); private arraylist arraylist1 = new arraylist(); private int id = 0; private int numpages; private char present; random random = new random(); int randomlevel = random.next(20, 30);//this line not work, if utilize randomlevel in in line below creating pupil objects pupil student = new student(); int maybe; public teacher() { int randomlevel = random.next(1, 3); id = uniqueid; absence = false; level = (randomlevel % 2 == 0); uniqueid++; randomlevel = random.next(20, 30); student[] pupil = new student[randomlevel]; maybe = randomlevel; (int = 0; < randomlevel; i++) { student[i] = new student(); } }
and here function in teacher uses student
public void addpages()//come need add together specific kid { int selection = 0; console.writeline("enter pupil id"); selection = int.parse(console.readline()); student[] student= new student[maybe];//if rid of line how take pupil object use. created new pupil object , not want student[choice] = new student(); int number = 0; if (student[choice].absent()) { number = student[choice].excused(); } else { console.writeline("how many pages did pupil read today? "); number = int.parse(console.readline()); } student[choice].add(number); }
is there way random work in declaration area above constructor?
i think problem comes downwards variable scope have provided quite bit of extraneous code. i'll simplify downwards think care about.
class teacher { // class-level list of students private list<student> _students = new list<student>(); public teacher() { var random = new random(); var studentcount = random.next(20,30); for(int = 0; < studentcount; i++) { _students.add(new student()); } } // utilize _students here on out public void addpages(...) { ... } }
c# class
Comments
Post a Comment