Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 在C中更新类实例变量的问题#_C# - Fatal编程技术网

C# 在C中更新类实例变量的问题#

C# 在C中更新类实例变量的问题#,c#,C#,在下面的代码中,systemUsers是一个绑定列表,usernames、firstNames和lastNames是数组 我正试图通过循环将新用户添加到绑定列表systemUsers。在添加它们之后,我会尝试更新它们的实例变量,如first name和last name 但是,当我这样做时,它们不会改变-它们的值为null。为什么会这样 public static void CreateUsers() { // loop over the num. of users for (

在下面的代码中,
systemUsers
是一个绑定列表,
usernames
firstNames
lastNames
是数组

我正试图通过循环将新用户添加到绑定列表
systemUsers
。在添加它们之后,我会尝试更新它们的实例变量,如first name和last name

但是,当我这样做时,它们不会改变-它们的值为
null
。为什么会这样

public static void CreateUsers() 
{
    // loop over the num. of users
    for (int i = 0; i < numberOfUsers; i++)
    {
        // create new user
        systemUsers.Add(new User(usernames[i]));

        // get class instance
        currentUser = systemUsers[i];

       // set class properties
        currentUser.FirstName = firstNames[i];
        currentUser.LastName = lastNames[i];
    }
}
publicstaticvoidcreateusers()
{
//在用户数上循环
for(int i=0;i

User()

试试下面的代码。您需要在循环中设置currentUser变量,然后设置其FirstName和LastName属性。现在,您没有在代码中的任何位置设置currentUser。使用变量而不声明并将其设置为某个值是不正确的

另外,请确保适当填充了
名字
姓氏
的数组,且不包含空值。例如,如果用户数为2个或更多用户,
firstNames[1]
不应为空

public static void CreateUsers() 
{
    //make sure currentUser variable is declared outside the loop
    //or inside the loop at start of the loop body 
    User currentUser = null;

   //make sure your systemUsers binding list is not null
    if(systemUsers == null) {
        systemUsers = new BindingList<User>();
    }

    // loop over the num. of users
    for (int i = 0; i < numberOfUsers; i++)
    {
        // create new user
        currentUser = new User(usernames[i]);
        currentUser.FirstName = firstNames[i];
        currentUser.LastName = lastNames[i];

       //add the User object to your list
        systemUsers.Add(currentUser);

    }
}
publicstaticvoidcreateusers()
{
//确保在循环外部声明currentUser变量
//或者在循环体开始处的循环内部
用户currentUser=null;
//确保systemUsers绑定列表不为空
if(systemUsers==null){
systemUsers=new BindingList();
}
//在用户数上循环
for(int i=0;i
您可以在实例化
用户
类的同时设置这些属性的值。无论
currentUser
引用什么,它都不是您在示例中实例化的
User

public static void CreateUsers() 
{
    for (int i = 0; i < numberOfUsers; i++)
    {
        systemUsers.Add(new User(usernames[i]) { FirstName = firstNames[i], LastName = lastNames[i] });
    }
}
publicstaticvoidcreateusers()
{
for(int i=0;i
将使用AddRange或直接初始化,如选择:

systemUsers = Enumerable.Range(0, numberOfUsers)
                        .Select(i => new User { 
                            FirstName = firstNames[i],
                            // others properties
                        })
                        .ToList()

您可以在currentUser中修改实例的属性,该属性不是您以前添加的属性;无法引用新用户实例。正如下面的答案所示,您需要将新实例存储在某个对象中,然后使用该对象的属性来保存数据值。另外,请确保源数组中有数据。我还添加了一些注释。systemUsers在进入循环之前是空的。不需要将currentuser的作用域置于循环之外。您是对的。它也可以放在循环中,以明确它只在循环中使用。