使用未赋值的局部变量组节点



不知道为什么我在 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
        }
    }

干杯

因为在使用该变量之前您没有显式启动该变量:

考虑这个可疑的代码:

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

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

要解决此问题,请在函数的开头写入

TreeNode groupNode = null;

TreeNode groupNode = new TreeNode();

编辑

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

TreeNode groupNode = default(TreeNode);

选择基于代码流逻辑。

groupNode初始化为null应该会有所帮助。

TreeNode groupNode = null;

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

if (groupNode != null)
{
     tree.Nodes.Add(groupNode);
     this.Controls.Add(tree);
} 
else
{
     //Do some logic because the variable is still null.
}

当你在iffor块中实例化groupNode时,你应该写:

TreeNode groupNode = null;

因为 C# 说它根本无法达到 iffor 块的那条线!

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

最新更新