Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何创建while循环来验证对象是否为null_C#_Validation - Fatal编程技术网

C#如何创建while循环来验证对象是否为null

C#如何创建while循环来验证对象是否为null,c#,validation,C#,Validation,我遇到了两个一起工作的验证问题,一个在主方法中,另一个在控制器类中 这是我在Main中的静态方法。基本上,我要求用户输入“Employee”的ID,ID是名为Employee的类的属性,然后我显示一条消息,说明该雇员是否存在。假设已经创建了几个Employee对象。我知道这是一种while循环,但我就是做不好 我在控制器类中的方法是正确的,因此不需要更改任何内容。基本上,如果Employee对象不存在,它接受ID并返回null;如果Employee对象确实存在,则返回该对象。如果员工不存在,我需

我遇到了两个一起工作的验证问题,一个在主方法中,另一个在控制器类中

这是我在Main中的静态方法。基本上,我要求用户输入“Employee”的ID,ID是名为Employee的类的属性,然后我显示一条消息,说明该雇员是否存在。假设已经创建了几个Employee对象。我知道这是一种while循环,但我就是做不好

我在控制器类中的方法是正确的,因此不需要更改任何内容。基本上,如果Employee对象不存在,它接受ID并返回null;如果Employee对象确实存在,则返回该对象。如果员工不存在,我需要将该值设为null,并创建一个循环,不断要求用户输入正确的ID,直到他输入为止,然后循环结束。谢谢,如果我没有正确解释的话,很抱歉

    public static void mymethod()
    {
        Console.WriteLine("Please enter your Employee ID: ");
        int ID = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("----------------------------");
        Employee employee = controllerclass.findemployee(ID);
        int i = 0;
        bool flag = false;
        while (!flag)
        {
            if (!flag)
            {
                if (ID != null)
                {
                    Console.WriteLine("The employee exists");
                    flag = true;
                }
                else
                {
                    Console.WriteLine("Please enter a valid ID");
                }
            }
            i++;
        }

我在控制器类中的方法是正确的,因此不需要更改任何内容。基本上,如果Employee对象不存在,它接受ID并返回null;如果Employee对象确实存在,则返回该对象。如果员工不存在,我需要将该值设为null,并创建一个循环,不断要求用户输入正确的ID,直到他输入为止,然后循环结束。谢谢,如果我没有正确解释,很抱歉

请使用do while循环,如下所示:

boolean flag = false;
do {
    if(flag)
        Console.WriteLine("Please enter a valid ID");
    else 
        Console.WriteLine("Please enter your Employee ID: ");
    try{
         int ID = Convert.ToInt32(Console.ReadLine());
    } catch (FormatException e){
        Console.WriteLine("Please enter an integer");
        flag = false;
        continue;
    } 
    employee = controllerclass.findemployee(ID);
    flag = true;
} while (employee == null) //employee will be null if not found
Console.WriteLine("The employee exists");

do while确保它至少发生一次。标志条件将发生在第一个条目之后;如果他们第一次做对了,那就永远不会发生

使用do while循环,类似这样:

boolean flag = false;
do {
    if(flag)
        Console.WriteLine("Please enter a valid ID");
    else 
        Console.WriteLine("Please enter your Employee ID: ");
    try{
         int ID = Convert.ToInt32(Console.ReadLine());
    } catch (FormatException e){
        Console.WriteLine("Please enter an integer");
        flag = false;
        continue;
    } 
    employee = controllerclass.findemployee(ID);
    flag = true;
} while (employee == null) //employee will be null if not found
Console.WriteLine("The employee exists");

do while确保它至少发生一次。标志条件将发生在第一个条目之后;如果他们第一次做对了,那就永远不会发生

一个细节是,
int
不能为null,因为它是一种值类型。您可以将其更改为
int?
,这是一个可为null的
int
——它可以有值,也可以为null

使“while”循环工作的关键是,无论它检查的是什么条件,都必须在循环内部进行更改

在这种情况下,
ID
在循环开始之前就被设置好了,循环中没有任何东西可以改变它

你可以这样做。(这比我通常做的要多一点嵌套,但它很有效。)

只要
employee
为空,循环就会执行。因此,在他们输入返回有效员工的内容之前,它将继续运行

我使用
int.TryParse
检查他们输入的值。如果输入的是整数,则返回
true
,否则返回
false
。这样,如果他们输入了一个有效的整数,我们将检查该员工。如果它甚至不是一个有效的整数,那么它会让他们知道


思考
while
循环的一种方法是,在退出循环之前,考虑您试图满足的条件。在这种情况下,您希望
employee
不是null。因此循环可以从
开始,而(employee==null)

一个细节是
int
永远不能为null,因为它是一种值类型。您可以将其更改为
int?
,这是一个可为null的
int
——它可以有值,也可以为null

使“while”循环工作的关键是,无论它检查的是什么条件,都必须在循环内部进行更改

在这种情况下,
ID
在循环开始之前就被设置好了,循环中没有任何东西可以改变它

你可以这样做。(这比我通常做的要多一点嵌套,但它很有效。)

只要
employee
为空,循环就会执行。因此,在他们输入返回有效员工的内容之前,它将继续运行

我使用
int.TryParse
检查他们输入的值。如果输入的是整数,则返回
true
,否则返回
false
。这样,如果他们输入了一个有效的整数,我们将检查该员工。如果它甚至不是一个有效的整数,那么它会让他们知道


思考
while
循环的一种方法是,在退出循环之前,考虑您试图满足的条件。在这种情况下,您希望
employee
不是null。因此,循环可以从
while(employee==null)

开始,只要您想至少执行一次某项操作,并且可能执行更多次,直到满足条件(找到id),就应该使用
do while
构造。下面是一些代码:

int id;
bool idIsNotNumeric = true;
do
{
    Console.WriteLine("Please enter your Employee ID: ");
    string idEntered = Console.ReadLine();
    idIsNotNumeric = !int.TryParse(idEntered, out id);

    // other stuff you want to keep doing

} while (idIsNotNumeric || controllerclass.findemployee(id) == null);

Console.WriteLine("The employee exists");

每当您想做某事至少一次,甚至可能多次,直到满足条件(找到id),您应该使用
do while
构造。下面是一些代码:

int id;
bool idIsNotNumeric = true;
do
{
    Console.WriteLine("Please enter your Employee ID: ");
    string idEntered = Console.ReadLine();
    idIsNotNumeric = !int.TryParse(idEntered, out id);

    // other stuff you want to keep doing

} while (idIsNotNumeric || controllerclass.findemployee(id) == null);

Console.WriteLine("The employee exists");

ID为int,所以它永远不会为null。这一点很好。我只是根据提问者的密码。我将编辑答案以反映这一点。非常感谢@jimboweb,我接受了您的代码并更改了一些内容,现在它完全按照我的要求工作。感谢您花时间帮助一个noobie.ID是int,所以它永远不会为null。这一点很好。我只是根据提问者的密码。我将编辑答案以反映这一点。非常感谢@jimboweb,我接受了您的代码并更改了一些内容,现在它完全按照我的要求工作。谢谢你花时间帮助一个noobie。谢谢你的回答,我能够解决这个问题。谢谢你的回答,我能够解决这个问题。