Question

I can't seem to access my private member variables through their public properties from a different class. I'm trying to instantiate some Student objects in StudentList through the Student Class. I've done it before but can't for the life of me remember or find anything that's working. I'm relatively new to programming so go easy on me.

Student Class Code

public partial class Student : Page
{
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double CurrentSemesterHours { get; set; }
    public DateTime UniversityStartDate { get; set; }
    public string Major { get; set; }
    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }

    new DateTime TwoYearsAgo = DateTime.Now.AddMonths(-24);

    public Boolean InStateEligable
    {
        get
        {
            if (UniversityStartDate < TwoYearsAgo) // at first glance this looks to be wrong but when comparing DateTimes it works correctly
            {
                return true;
            }
            else { return false; }
        }
    }
    public decimal CalculateTuition()
    {
        double tuitionRate;
        if (InStateEligable == true)
        {
            tuitionRate = 2000;
        }
        else
        {
            tuitionRate = 6000;
        }
        decimal tuition = Convert.ToDecimal(tuitionRate * CurrentSemesterHours);
        return tuition;
    }

    public Student(int studentID, string firstName, string lastName, double currentSemesterHours, DateTime universityStartDate, string major)
    {
            StudentID = studentID;
            FirstName = firstName;
            LastName = lastName;
            CurrentSemesterHours = currentSemesterHours;
            UniversityStartDate = universityStartDate;
            Major = major;
        }
    }

StudentList class code is basically blank right now. I've been messing around with it trying to get intellisense to access my other class but no luck so far. I must be missing something simple.

public partial class StudentList : Page
{
}    
Was it helpful?

Solution 3

I found the simple solution. I was trying to access the code behind of one page from another, which as many of you have pointed out won't play nice. By moving the code into it's own c# class within the App_Code folder it becomes accessible to everything. Thanks for the help!

OTHER TIPS

First, to answer your question:

"I can't seem to access my private member variables through their public properties from a different class..."

That's exactly why they're called private. Private members are accessible only within the class that declared them, and you must use the public properties to get access from other classes.

Now, some recommendations:

1) Avoid using the same class as a Code Behind and a domain model class. I strongly advice you create a separate "Student" class only with the properties/business methods, and leave the code behind as a separate "StudentPage" class. That makes your code easier to work with because the different concerns are separated (view logic x business logic), and because each of these classes should have different lifetimes.

2) Instead of:

private int StudentID;
public int studentID
{
    get
    {
        return StudentID;
    }
    set
    {
        StudentID = value;
    }
}

... you can write Automatic Properties:

public int StudentId { get; set; }

The point in here is web application is stateless application, so the lifetime of each web page is in per request lifetime.

In your code Student and StudentList are web pages, so from StudentList you cannot access instance of Student because it does not live anymore.

Therefore, consider using Session to transfer data between pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top