Click here to Skip to main content
13,895,757 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello.
I have my class called Students. And I keep the failstudents variable in class like this.
I would like to call this variable in another class and ı use want to if statement.
Do I call it that way.

How to call another class and if statement.




public list <string> failstudents=new list<string>();

What I have tried:

Students.cs

public list <string> failstudents=new list<string>();

Process.cs

İf(Students.failstudents.count !=0)
{

}
Posted
Updated 17-Feb-19 17:02pm
Comments
Maciej Los 17-Feb-19 15:07pm
   
Does Student object is just a string?
EngineerSC 17-Feb-19 15:25pm
   
failstudents keep fail list.I want to fail list use if statement and use other class.maybe ı told you wrongly at top.But I want to use this list in another class to calculate.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There are two types of "elements" that a class can have: static elements, and instance elements (where an element is a field, property, method, event, or delegate)

A static element is shared by all instances, and is accessed via the class name.
An instance element is unique to each different instance of the class and is accessed via the variable holding the instance reference.

Think about cars for a moment: all cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean.
But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car)

So if you want to access a List via the class name as in your example, it needs to be common to all instances, i.e. it needs to be static :
public static List<string> failstudents = new List<string>();
   
v2
Comments
BillWoodruff 19-Feb-19 1:33am
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

imho, one possible way to make your intent more clear is to distinguish between an entity/Class that represents an instance and an entity/Class that represents a collection of instances, and/or management of instances.

Consider:
public class Student
{
    // ctor
    public Student(string name)
    {
        Name = name;
        StudentManager.RegisterStudent(this);
    }

    // fields that hold student data

    public bool IsPassing { get; private set; }

    // ... more student data
    public string Name { get; private set; }

    private double _averagegrade;
    public double AverageGrade {
        get
        {
            return _averagegrade;
        }

        internal set // for future: find a way to only allow Manager class to set this
        {
            _averagegrade = value;

            IsPassing = _averagegrade > StudentManager.FailLevel;

            if (! IsPassing)
            {
                // show a warning ?
                throw new ArgumentOutOfRangeException("", "\r\r\r\nAverage Grade: FAILING\r\n");
            }
        }
    }

    public void AddGrade(double grade)
    {
        StudentManager.AddGrade(this, grade);
    }
}

public static class StudentManager
{
    public static double FailLevel = 50.0;

    // map students to grades
    public static Dictionary<Student, List<double>> StudentGrades = new Dictionary<Student, List<double>>();

    // ctor
    public static void RegisterStudent(Student student)
    {
       // left for you to implement
    }

    public static void AddGrade(Student student, double grade)
    {
       // left for you to implement
    }

    public static double GetAverageGrade(Student student)
    {
        var grades = StudentGrades[student];
        return grades.Sum() / grades.Count;
    }

    public static int NumberOfStudents
    {
        get { return StudentGrades.Count; }
    }

    public static List<Student> GetFailingStudents
    {
       // left for you to implement
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 17 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100