【问题标题】:The function executes again even after coming out of the for loop in asp.net即使退出 asp.net 中的 for 循环,该函数也会再次执行
【发布时间】:2014-05-20 11:06:35
【问题描述】:

我在我的网络项目中使用TreeView 来显示菜单。下面是我用来查找节点级别的函数。

int FindLevel(string id, int level)
{
    int dj = 0;
    for (dj = 0; dj < dTable.Rows.Count; dj++)
    {
        string id2 = dTable.Rows[dj]["TCode"].ToString();
        string parent1 = dTable.Rows[dj]["TGroup"].ToString();
        if (id == id2)
        {
            if (parent1 == "")
            {
                return level;
            }
            else
            {
                level += 1;
                FindLevel(parent1, level);
            }
        }
    }
    return level;
}

private void PopulateRootLevel()
{
    dTable = getConnection.GetAdapterDatatable("uspMenuGet 'ALL','HVV',0,0,0", csGlobal.getMainConnection);
    int di = 0;
    for (di = 0; di < dTable.Rows.Count; di++)
    {
        string id1 = dTable.Rows[di]["TCode"].ToString();
        dTable.Rows[di]["TLevel"] = FindLevel(id1, 0);
    }
}

让我们假设当 dj=10 时,第一个 if 条件满足并在返回级别时退出 for 循环。 但是,在退出 for 循环后,FindLevel 函数会再次执行,直到 dj 达到 Datatable 计数。

【问题讨论】:

    标签: asp.net treeview treenode


    【解决方案1】:

    return 将返回值,但循环将继续。

    使用 break 退出循环。

    【讨论】:

    • 我使用了休息时间。但是,它并没有打破循环。它只是中断当前迭代并继续。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2021-11-18
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多