如何通过获取 ID 显示名称?
How do i display Name by getting ID?
我问用户 his/her 选民的 ID 是什么,如果它在我的列表中,程序会显示一个显示 his/her 姓名的消息框。然后如果用户按下 "ok" 它会重定向到另一个表单。
这是我的代码:
public class Voter{
public string voterName {get; set;}
public int voterID {get; set;}
public override string ToString()
{
return " Name: " + voterName;
}
}
void BtnValidateClick(object sender, EventArgs e)
{
int id = Int32.Parse(tbVotersID.Text);
List<Voter> voters = new List<Voter>();
voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});
if (voters.Contains(new Voter {voterID = id})){
//prompts a messagebox that shows voterName
}
else{
MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
您可以将 LINQ 与 Find() 结合使用来获取第一个结果的 voterName。检查它是否为空,如果不是,则显示 MessageBox()。 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=netframework-4.8
void BtnValidateClick(object sender, EventArgs e)
{
int id = Int32.Parse(tbVotersID.Text);
List<Voter> voters = new List<Voter>();
voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});
var voterName = voters.Find(voter => voter.voterID == id)?.voterName;
if (!string.IsNullOrEmpty(voterName)){
MessageBox.Show(voterName);
}
else{
MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
我问用户 his/her 选民的 ID 是什么,如果它在我的列表中,程序会显示一个显示 his/her 姓名的消息框。然后如果用户按下 "ok" 它会重定向到另一个表单。
这是我的代码:
public class Voter{
public string voterName {get; set;}
public int voterID {get; set;}
public override string ToString()
{
return " Name: " + voterName;
}
}
void BtnValidateClick(object sender, EventArgs e)
{
int id = Int32.Parse(tbVotersID.Text);
List<Voter> voters = new List<Voter>();
voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});
if (voters.Contains(new Voter {voterID = id})){
//prompts a messagebox that shows voterName
}
else{
MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
您可以将 LINQ 与 Find() 结合使用来获取第一个结果的 voterName。检查它是否为空,如果不是,则显示 MessageBox()。 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=netframework-4.8
void BtnValidateClick(object sender, EventArgs e)
{
int id = Int32.Parse(tbVotersID.Text);
List<Voter> voters = new List<Voter>();
voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});
var voterName = voters.Find(voter => voter.voterID == id)?.voterName;
if (!string.IsNullOrEmpty(voterName)){
MessageBox.Show(voterName);
}
else{
MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}