Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:48:01+00:00 2026-05-28T07:48:01+00:00

I have two classes Teacher and Student . I am trying to get teacher

  • 0

I have two classes Teacher and Student. I am trying to get teacher to use the class functions in students. The problem I am having is that I need the number of student object being used in Teacher to be random. I thought I figured this out in the constructor, but I had to declare new student object in every function of teacher that I use student. So that just creates a new student object which does me no good. Here is my 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 does not work, if I could then I would just use randomLevel in the in the line below for creating my student objects
    Student 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[] student = new Student[randomLevel];
        maybe = randomLevel;
        for (int i = 0; i < randomLevel; i++)
        {
            student[i] = new Student();
        }
    }

and here is a function in teacher that uses student

  public void addPages()//Come back need to add specific child
  {
        int choice = 0;
        Console.WriteLine("Enter the student ID");
        choice = int.Parse(Console.ReadLine());
        Student[] student= new Student[maybe];//If i get rid of this line then how will I choose which student object to use.  However this created a new student object and I do not want to do that

        student[choice] = new Student();
        int number = 0;
        if (student[choice].absent())
        {
            number = student[choice].excused();
        }
        else
        {

            Console.WriteLine("How many pages did the student read today? ");
            number = int.Parse(Console.ReadLine());
        }
        student[choice].add(number);           
   }

Is there a way to get the random to work in the declaration area above the constructor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:48:02+00:00Added an answer on May 28, 2026 at 7:48 am

    I think your problem comes down to variable scope but you have provided quite a bit of extraneous code. I’ll simplify down to what I think you actually 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 i = 0; i < studentCount; i++)
            {
                _students.Add(new Student());
            }
        }
    
        // use _students from here on out
        public void AddPages(...) { ... }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, a Teacher class and a Student class. In my program
I have two classes that extend the activity class. Each class has it's own
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have two classes(Book,Software) that inherit from the Product class. these two classes have
I have two classes: class Player { public string Id { set; get; }
I have two classes that don't know anything about themselfs class A, class B.
I have two classes, Student and Teacher, Student has one concrete method: takeCourse; Teacher
I have two classes, Phone.class and Time.class. I would like to use their methods
I have two classes public class A { public int BaseA {get;set;} } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.