应用程序在扫描阵列时崩溃

时间:2014-06-06 17:02:34

标签: c# windows-phone

我有一个二维的字符串数组。当我尝试扫描数组中的项目时,我的应用程序崩溃了。

示例:查看员工列表,在查看员工时,用户可以按“开火”按钮终止雇佣。如果在终止后剩下不到1名员工,则不应该允许这样做。 某人必须在附近经营这项业务。

int index = 0; //counter for the array
string[,] employee = { {"Andy","CEO","true"},
                       {"Bill","HR","true"},
                       {"Carl","Janitor","false"}
                     }; //name, title, employed?

...

private void btnFire_Click(object sender, RoutedEventArgs e){
    int employed = 0; //counter for number of employed
    for (int i = 0; i<employee.Length; i++){
        if (employee[i,2] == "true") employed++;
    }
    if (employed > 1) employee[index,2]="false";
}

为什么应用程序无法处理此操作的任何想法?我唯一可以想到的是,子函数不允许修改来自它上面的层次结构级别的数组。是否需要以不同的方式声明它?或者我是否可以解决问题所在?

4 个答案:

答案 0 :(得分:4)

这是一个二维数组

Employee.Length将返回9,这就是您的应用崩溃的原因;你必须使用GetLength(0)来做你想做的事;这将返回数组第一维的长度。

        for (int i = 0; i < employee.GetLength(0); i++)
        {
            if (employee[i, 2] == "true")
            {
                employed++;
            }
        }

正如其他人所说,Employee类和List会使这个更清洁。

public class Employee
{
    public string Name { get; set; }
    public string Title { get; set; }
    public bool Employed { get; set; }
}

然后你可以使用LINQ来获得你的就业数。

        var employees = new List<Employee>() 
        { 
            new Employee {Name = "Andy", Title = "CEO", Employed = true},
            new Employee {Name = "Bill", Title = "HR", Employed = true},
            new Employee {Name = "Carl", Title = "Janitor", Employed = false}
        };

        var employed = (from e in employees where e.Employed == true select e).Count();

答案 1 :(得分:1)

您需要使用GetLength(0)代替Length来获取第一个尺寸。

话虽这么说,这是一种可怕的方式来接近你正在做的事情。重新开始,马上重新开始。建立一个员工类:

public class Employee
{
    public string Name {get; set;}
    public string Title {get; set;}
    public bool Employed {get; set;} //Note this is an actual bool, not a string!

    public Employee(string name, string title, bool employed) //Custom constructor
    {
        Name = name;
        Title = title;
        Employed = employed;
    }

    public void Fire()
    {
        Employed = false;
    }
}

然后进行简单的计数检查并调用fire函数:

List<Employee> employees = { new Employee("Andy","CEO", true),
                       new Employee("Bill","HR",true),
                       new Employee("Carl","Janitor",false)
                     }; 

...

private void btnFire_Click(object sender, RoutedEventArgs e){
    if (employees.Count(e => e.Employed) > 1) //Count Employed employees
        employees[index].Fire(); //Not sure where index comes from...
}

答案 2 :(得分:1)

代码的问题是employee.Length是9。

您可能想获得行的长度:

employee.GetLength(0)

答案 3 :(得分:1)

public class Employee
{
    public string Name { get; set; }
    public string Title { get; set; }
    public bool Employed { get; set; }
}

List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { Name = "Andy", Title = "CEO", Employed = true });
empList.Add(new Employee() { Name = "Bill", Title = "HR", Employed = true });
empList.Add(new Employee() { Name = "Carl", Title = "Janitor", Employed = false });

var firedemplist = empList.Where(x => x.Employed == false);
var newemplist = empList.Except(firedemplist);