0

不知道为什么我会在 Webpart 中为 treeView 收到此错误,老实说,这可能是一个逻辑错误,

tree.Nodes.Add(groupNode);

为什么会这样说:S

        protected override void CreateChildControls()
    {
        base.CreateChildControls();

        try
        {
            int Index = 0;
            TreeView tree = new TreeView();
            TreeNode groupNode; 
            Dictionary<int, string> GroupList = new Dictionary<int, string>();
            Dictionary<int, string> UserList = new Dictionary<int, string>();
            List<string> IndividualUserList = new List<string>();

            foreach (SPUser user in SPContext.Current.Web.Users)
            {
                string groupName = FormatUserLogin(user.Name);

                if (groupName != "" && groupName != "System Account")
                    IndividualUserList.Add(groupName);
                else if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) && 
                    Directory.DoesGroupExist(groupName))
                {
                    Index++;
                    GroupList.Add(Index, groupName);
                    List<ADUser> adUsers = Directory.GetUsersFromGroup(groupName);

                    foreach (ADUser member in adUsers)
                    {
                        if (member != null && !string.IsNullOrEmpty(member.DisplayName))
                            UserList.Add(Index, member.DisplayName);
                    }
                }
            }

            IndividualUserList.Sort();

            foreach (string Item in IndividualUserList)
            {
                groupNode = new TreeNode(Item);
            }

            foreach (KeyValuePair<int, string> GroupPair in GroupList)
            {
                groupNode = new TreeNode(GroupPair.Value);
                foreach (KeyValuePair<int, string> UserPair in UserList)
                {
                    if (UserPair.Key == GroupPair.Key)
                    {
                        groupNode.ChildNodes.Add(new TreeNode(UserPair.Value));
                    }
                }
            }

            tree.Nodes.Add(groupNode);

            this.Controls.Add(tree);
        }
        catch (Exception)
        {
            //loggingit
        }
    }

干杯

4

5 回答 5

1

因为您在使用它之前没有明确初始化该变量:

考虑到这个可疑代码:

foreach (string Item in IndividualUserList)
{
    groupNode = new TreeNode(Item);
}

目前尚不清楚为什么您需要在整个迭代过程中初始化同一个实例,但是顺便说一下,没有任何保证IndividualUserList为空,因此变量可以保持未初始化

为了解决这个问题,在函数的开头,写

TreeNode groupNode = null;

或者

TreeNode groupNode = new TreeNode();

编辑

或者,正如弗拉德建议的那样,可以选择:

TreeNode groupNode = default(TreeNode);

选择基于您的代码流逻辑。

于 2012-05-10T09:45:06.863 回答
0

初始化groupNode应该有null帮助。

TreeNode groupNode = null;
于 2012-05-10T09:44:46.413 回答
0

如果您的循环不执行,groupNode则永远不会分配值 for。你可以做的是做这样的事情:TreeNode? groupNode = null;. 这将为变量提供一个初始值,并将删除错误/警告。但是,我建议进行以下更改:

if (groupNode != null)
{
     tree.Nodes.Add(groupNode);
     this.Controls.Add(tree);
} 
else
{
     //Do some logic because the variable is still null.
}
于 2012-05-10T09:45:31.190 回答
0

当您仅在iffor块中实例化groupNode时,您应该编写:

TreeNode groupNode = null;

因为 C# 说它可能根本无法到达iffor块的那一行!

于 2012-05-10T09:47:20.343 回答
0

仅当第二个或第三个 for 循环有要迭代的内容时,它才有价值。如果没有要迭代的内容,应该将什么添加到Nodes集合中?在这种情况下,编译器希望确保您的意思是null(?)

于 2012-05-10T09:49:12.413 回答