为什么我的foreach中的代码无法访问? (被单元测试的工作代码的确切副本) [英] Why is the code in my foreach unreachable? (Exact copy of working code thats been Unit tested)

查看:218
本文介绍了为什么我的foreach中的代码无法访问? (被单元测试的工作代码的确切副本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是一个完美的代码的精确副本。不同的是,这个代码被放置在WCF服务应用程序项目中,而工作代码来自Windows窗体应用程序项目。在foreach中的代码是无法访问的,这是奇怪的,因为我已经测试了代码之前,它的工作原理,返回正确的值

 公共IEnumerable<员工> GetStudentDetails(字符串用户名,字符串密码)
{
var emp = agrDb.LoginAuthentication(username,password); //数据库中返回两个值的过程
//即:EmployeeFirstName和EmployeeLastName
列表<员工> trainerList =新列表< Employee>();

foreach($ emp $)

$ unreachable code
雇员聘用=新员工();
employ.EmployeeFirstName = item.EmployeeFirstName;
employ.EmployeeLastName = item.EmployeeLastName;
trainerList.Add(employ);
//trainerList.Add (item.EmployeeLastName);
}
return trainerList;


解决方案

如果数组或集合直到运行时才被初始化,那么该数组或集合是不可删除的。

 列表< Employee> EMP; 

//程序启动时运行,从Program.cs调用
private void InitialiseApplication()
{
emp = new List< Employee> ;;

//从某处收集员工的数据。
DataAccess.GetEmployees(emp);


private void DoStuff()
{
foreach($ emp $)

//做一些事情。






$ b上面的代码会带来警告,因为emp 在设计时没有初始化。

我在代码中收到了相同的警告,包括构造函数在内的各个阶段。但是,运行时流程不受影响,因为到那时,emp被初始化。

这可能是您的代码的情况。检查在程序emp被初始化的过程中何时何地。如果看不见,你可能需要步入该计划来实现这一目标。


The code below is an exact copy of code that's working perfectly. The difference is that this code is being placed in a WCF Service Application Project whereas the working code is from a Windows Forms Application Project. The code in the foreach is unreachable which is strange because I've tested the code before and it works, returning the correct values

public IEnumerable<Employee> GetStudentDetails(string username,string password)
    {
        var emp = agrDb.LoginAuthentication(username, password);//procedure in the database thats returning two values
                                                                //Namely: EmployeeFirstName and EmployeeLastName
        List<Employee> trainerList = new List<Employee>();

        foreach (var item in emp)
        {
            //unreachable code here
            Employee employ = new Employee();
            employ.EmployeeFirstName = item.EmployeeFirstName;
            employ.EmployeeLastName = item.EmployeeLastName;
            trainerList.Add(employ);
            //trainerList.Add(item.EmployeeLastName);
        }
        return trainerList;
    }

解决方案

The code within foreach loops can be uncreachable if the array or collection is not initialised until runtime.

List<Employee> emp;

// Run when program starts, called from Program.cs
private void InitialiseApplication()
{
    emp = new List<Employee>;

    // Gather data for employees from... somewhere.
    DataAccess.GetEmployees(emp);
}

private void DoStuff()
{
    foreach (var item in emp)
    {
         // Do something.
    }
}

The above code will bring back the warning because "emp" is not initialised at design time.

I've received the same warning in my code, at various stages including within constructors. However, the runtime flow is not affected because by then, "emp" is initialised.

This may be the case with your code. Check to see where and when during the flow of the program "emp" is initialised. You may need to "step into" the program to acheive this, if it is not obvious by sight.

这篇关于为什么我的foreach中的代码无法访问? (被单元测试的工作代码的确切副本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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