0

我想将所有用户配置文件的多个字符串值存储到 Array 对象中。我的代码是:

                    if (upm != null)
                    {
                        for (int i = 0; i < otherMembers.Count; i++)
                        {
                            UserProfile user = upm.GetUserProfile(otherMembers[i]);


                            int j = 0;
                            if (user[FieldNames.OtherDepartment].Count != 0)
                            {

                                UserProfileValueCollection valueCollection = user[FieldNames.OtherDepartment];
                                Array otherDepartments = Array.CreateInstance(typeof(string), user[FieldNames.OtherDepartment].Count);
                                foreach (object value in valueCollection)
                                {
                                    otherDepartments.SetValue(value.ToString(), j);
                                    j++;
                                }

                                j = otherDepartments.Length;
                                otherDepartments.SetValue(fieldValue.Label, j);

                                user[FieldNames.OtherDepartment].Value = otherDepartments;
                            }
                            else
                            {
                                user[FieldNames.OtherDepartment].Value = fieldValue.Label;
                            }
                            //user[FieldNames.OtherDepartment].Value += fieldValue.Label;
                            user.Commit();
                        }
                    }

我在这条线上得到了错误:otherDepartments.SetValue(fieldValue.Label, j);forech()循环之外。我错过了什么?

4

1 回答 1

0

您的代码中有一个错误。

尝试改变这一点:

j = otherDepartments.Length;

进入这个:

j = otherDepartments.Length-1;

由于数组索引通常从 0 开始,所以最后一个元素是length-1.

于 2013-10-21T09:02:39.143 回答