比较同一索引中列表中的两个项目。 [英] Compare two items in a list in the same index.

查看:66
本文介绍了比较同一索引中列表中的两个项目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

List<Person> people = new List<>





对象人员如下所示:



Object Person looks as follows:

public string Name{ get; set; }
public string Surname{ get; set; }





我尝试过:



如何比较Name和Surname的两个值?

例如,索引0处的List有:

Name =blah

姓氏=nah



例如:如果名字'blah'与姓氏'nah'匹配相同的索引做某事。



编辑:



假设我有两个文本框,一个文本框显示名称,用户必须输入姓氏。姓氏需要匹配Name所在的索引。因此,如果Name是'blah'并且用户在Surname中输入已经是'Nah'(在同一索引中),那么就做一些事情,例如显示MessageBox。你可以把它视为一个猜谜游戏,他的姓氏/名字是什么。



我忘了提到List项目是随机生成的。以下是代码:





What I have tried:

How could I compare the two values of Name and Surname?
for example, List at index 0 have:
Name = "blah"
Surname = "nah"

For example: if name 'blah' matches the same index as surname 'nah' do something.



Lets say I have two textboxes, one textbox displays the Name and the user have to type in the surname. The surname needs to match the same index as the Name is in. So if Name is 'blah' and the user types in the Surname which is already 'Nah'(in the same index) then do something, for example display MessageBox. You can treat that as a guessing game, what is his surname/name.

I forgot to mention that List item is being randomly generated. Here is the code:

randomPerson= people[random.Next(people.Count)];
string result = RandomlyGenerated(random);





是随机方法:





This is the random method:

private string RandomlyGenerated(Random r)
        {
            if (random.Next(1) == 0)
            {
                txtName.Text = randomPerson.Name;
                return randomPerson.Name;
            }
            else
            {
                txtSurname.Text = randomPerson.Surname;
                return randomPerson.Surname;
            }
        }





也许这样会更清楚。基本上,如果txtName显示文本。用户必须猜测与屏幕上显示的名称在同一索引中的相应姓氏是什么。



因此,如果我们在索引0中:

姓名='blah'

姓氏='nah'



用户应输入'nah'作为姓氏,例如出现的消息框。希望它现在能让它更清晰。



还在苦苦挣扎:/。



Maybe this way it will be more clear. Basically if txtName displays text. The user have to guess what is the corresponding surname that is in the same index as the name which was displayed on screen.

So if we have in Index 0:
Name = 'blah'
Surname = 'nah'

The user should enter 'nah' as the surname to, for example message box to appear. Hope it will make it more clearer now.

Still struggling with it :/.

推荐答案

注意:这段代码编写得很快,而且,当它编译时,我还没有彻底测试过。



首先,如果你的代码正在创建'Person的实例列表,你想要执行一些筛选规则姓名,这是一个例子:
Note: this code was written quickly, and, while it compiles, I have not tested it thoroughly.

First, if your code is creating the list of instances of 'Person and you want to enforce some screening rule about names, here's an example:
public class Person
{
    private static List<string> surnamesInUse = new List<string>();
    private static List<string> firstNamesInUse = new List<string>();

    private string lname;
    private string lsurname;

    public Person(string name, string surname)
    {
        if (name == String.Empty || surname == String.Empty)
        {
            throw new ArgumentException(


name和surname不能为空字符串);
}

lname = name.ToLower();
lsurname = surname.ToLower();

if(lname == lsurname)
{
抛出新的ArgumentException(
"name and surname cannot be empty strings"); } lname = name.ToLower(); lsurname = surname.ToLower(); if (lname == lsurname) { throw new ArgumentException(


名称和姓氏不能独立于案例) ;
}

if(firstNamesInUse.Contains(lname))
{
抛出新的DuplicateNameException(
"name and surname cannot match independent of case"); } if (firstNamesInUse.Contains(lname)) { throw new DuplicateNameException(


这篇关于比较同一索引中列表中的两个项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆