【发布时间】:2016-08-03 20:35:47
【问题描述】:
我必须为我的班级创建一个程序,我几乎完成了。我将此代码链接到一个名为 PersonEntry 的类。我试图找出为什么我的 EndOfStream 不起作用。它说它不能用作方法
namespace Jason_Todd___IS_204___HW11CH9_7
{
public partial class Form1 : Form
{
string selectedName = "";
List<PersonEntry> nameList = new List<PersonEntry>();
public Form1()
{
InitializeComponent();
}
// gotta load the names from this list. Because, you know, I need to get a grade.
private void Form1_Load(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
inputFile = File.OpenText("Personlist.txt");
string inRecord;
while (!inputFile.EndOfStream()) // This is where my error occurs.
{
inRecord = inputFile.ReadLine();
string[] tokens = inRecord.Split(',');
PersonEntry friendObj = new PersonEntry(tokens[0],tokens[1],tokens[2]);
listBox1.Items.Add(friendObj.Name);
nameList.Add(friendObj);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("Exception in try/catch. ");
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedName = listBox1.SelectedItem.ToString();
PersonInfoForm myPerInfoForm = new PersonInfoForm();
Label label1 = new Label();
label1.Size = new Size(270, 75);
label1.Location = new Point(10, 10);
foreach (PersonEntry PersonEntry in nameList)
{
if (PersonEntry.Name == selectedName)
{
label1.Text += "Name: " + PersonEntry.Name + "\n" +
"Email: " + PersonEntry.Email + "\n" +
"Phone number: " + PersonEntry.PhoneNum;
}
}
myPerInfoForm.Controls.Add(label1);
myPerInfoForm.ShowDialog();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
【问题讨论】:
-
EndOfStream是一个属性,而不是一个方法。只需删除()即可编译。