我正在尝试验证用户名复选框,以查看输入的值是否存在于XML文件中。
单击按钮,它应检查输入的名称是否存在于XML文件中,然后继续,否则应显示消息框。
当前代码显示由于其保护级别,txt_Username.Text = Pupil.forename无法访问。
点击按钮:
private void btnNext_Click(object sender, RoutedEventArgs e, Pupil p)
{
if (txt_Username.Text = Pupil.forename)
{
this.Hide();
Display nw = new Display(theClass);
nw.ShowDialog();
this.Show();
}
MessageBox.Show("Cannot Find username");
}
学生班:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PassingData
{
public class Pupil
{
private string forename;
private int score;
public Pupil(string forename, int score)
{
this.forename = forename;
this.score = score;
}
public Pupil()
{
this.forename = "Unknown";
}
public string Forename
{
get { return forename; }
set { forename = value; }
}
public int Score
{
get { return score; }
set { score = value; }
}
override public string ToString()
{
string output = forename + "\t" + score;
return output;
}
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPupil xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Pupil>
<Forename>Andy</Forename>
<Score>0</Score>
</Pupil>
<Pupil>
<Forename>Bob</Forename>
<Score>10</Score>
</Pupil>
<Pupil>
<Forename>Carl</Forename>
<Score>20</Score>
</Pupil>
<Pupil>
<Forename>Dave</Forename>
<Score>30</Score>
</Pupil>
<Pupil>
<Forename>Eric</Forename>
<Score>40</Score>
</Pupil>
<Pupil>
<Forename>Frank</Forename>
<Score>50</Score>
</Pupil>
</ArrayOfPupil>
答案 0 :(得分:0)
Pupil
不是静态类您需要创建Pupil
的实例并改为使用Forename
属性。
即
在非静态类上不允许Pupil.Forename
。
相反
Pupil objPupil = new Pupil(); var myForeName = objPupil.Forename;
在If
声明中,只有一个=
。
Pupil p
。答案 1 :(得分:0)
我还推荐仅用于设置和获取值的自动属性:http://msdn.microsoft.com/en-us/library/bb384054.aspx。
前:
public string Forename { get; set; }