我似乎無法通過其他類的公共屬性訪問我的私有成員變量。我試圖通過學生類在StudentList
中實例化一些Student
對象。我已經做過,但不能爲我的生活記住或找到任何有用的東西。我對編程比較陌生,所以對我來說很簡單。通過公共屬性訪問私有變量
學生類代碼
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類代碼,現在基本上是空白。我一直在試圖讓intellisense訪問我的其他課程,但沒有運氣到目前爲止。我必須錯過簡單的東西。
public partial class StudentList : Page
{
}
你有你的班級的實例訪問? – 2013-02-28 04:04:09
你想從StudentList訪問學生? – 2013-02-28 04:04:46
@ cuong le,是 – Milne 2013-02-28 04:05:39