KuDo
KuDo

Reputation: 21

Issues Updating Class Instance Variables in C#

In the code below, systemUsers is a binding list and usernames, firstNames and lastNames are arrays.

I'm trying to add new users to the binding list systemUsers through a loop. After they have been added, I then attempt to update their instance variables such as first name and last name.

However, when I do this, they do not change - they take on a value of null. Why is this?

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];
    }
}

User() is a constructor which takes the parameter 'username' i.e. User(string username).

Upvotes: 1

Views: 1414

Answers (3)

Drag and Drop
Drag and Drop

Reputation: 2734

Will either an AddRange or a direct Initialisation using a Select like :

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

Upvotes: 0

Sunil
Sunil

Reputation: 21396

Try the code below. You need to set the currentUser variable in your loop and then set their FirstName and LastName properties. Right now, you are not setting currentUser anywhere in your code. It is not correct to use a variable without declaring and setting it to some value.

Also, make sure that the arrays of firstNames and lastNames are appropriately populated and do not contain null values. For example, firstNames[1] should not be null if number of users is 2 or more users.

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);

    }
}

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66439

You can set the values of those properties at the same time you instantiate the User class. Whatever currentUser is referencing, it's not the User class your instantiating in your example.

public static void CreateUsers() 
{
    for (int i = 0; i < numberOfUsers; i++)
    {
        systemUsers.Add(new User(usernames[i]) { FirstName = firstNames[i], LastName = lastNames[i] });
    }
}

Upvotes: 1

Related Questions