【问题标题】:Use of unassigned local variable groupNode使用未分配的局部变量 groupNode
【发布时间】:2012-05-18 20:51:48
【问题描述】:

不知道为什么我会在 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 循环中将新的 TreeNode 重新分配给您的 groupNode。我无法想象这会产生你想要的结果。
  • 我不太确定这个 treeNode tbh 在做什么,你可以叫我菜鸟
  • 不用担心。我们都去过那里!

标签: c# .net treeview web-parts nodes


【解决方案1】:

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

考虑到这个可疑代码:

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

不清楚为什么您需要在整个迭代过程中初始化 same 实例,但顺便说一句,没有人保证 IndividualUserListnot 空的,所以变量可以保持未初始化

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

TreeNode groupNode = null;

TreeNode groupNode = new TreeNode();

编辑

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

TreeNode groupNode = default(TreeNode);

选择取决于您的代码流逻辑。

【讨论】:

  • 我建议 TreeNode groupNode = default(TreeNode); (如果它是一个结构)
【解决方案2】:

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

TreeNode groupNode = null;

【讨论】:

  • 它建立起来但给出了这个错误,“索引超出范围。必须是非负数并且小于集合的大小。参数名称:索引”这可能是我正在做的逻辑错误://
【解决方案3】:

如果您的循环不执行,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.
}

【讨论】:

  • 感谢 Npinti 它解决了 Indexcoming Null 问题,但现在什么都没有出现,这让我觉得,我没有正确开发树视图。
  • @TimeToThine:我建议您引入断点以查看代码的哪些段正在运行,哪些未运行。我建议您再问一个问题,为什么屏幕上没有任何内容:)
  • 如果我的声誉超过 125,我很想再问一个问题,我现在要调试它
【解决方案4】:

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

TreeNode groupNode = null;

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

【讨论】:

    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 2015-11-09
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多