将vb转换为c#(类型和标识符在foreach语句中都是必需的)

时间:2015-06-26 02:09:53

标签: c#

vb代码:

Dim janTbl As Janus.Windows.GridEX.GridEXTable

For Each janTbl In lvtoolpartsinfo.Tables` 

c#c​​ode:

Janus.Windows.GridEX.GridEXTable janTbl = new Janus.Windows.GridEX.GridEXTable();

foreach (janTbl in lvtoolpartsinfo.Tables)
{    }

错误:foreach语句中都需要类型和标识符

编辑:

c#c​​ode:

foreach (Janus.Windows.GridEX.GridEXTable janTbl in lvtoolpartsinfo.Tables)
    janTbl.Columns[0].Visible = false;
    janTbl.Columns[1].Caption = "Part Number";
    janTbl.Columns[1].ShowRowSelector = true;
    janTbl.Columns[1].EditType = Janus.Windows.GridEX.EditType.NoEdit;
    janTbl.Columns[1].Width = 120;

错误:

line 3:The name 'janTbl' does not exist in the current context
line 4 ...
line 5..

2 个答案:

答案 0 :(得分:1)

删除此行:

Janus.Windows.GridEX.GridEXTable janTbl = new Janus.Windows.GridEX.GridEXTable();

然后,您只需要修复foreach语句,以包含janTbl的类型,然后设置:

foreach (Janus.Windows.GridEX.GridEXTable janTbl in lvtoolpartsinfo.Tables) 
{
    // ...
}

答案 1 :(得分:1)

你缺少大括号:

foreach (var janTbl in lvtoolpartsinfo.Tables)
{ // <--- don't forget this

    janTbl.Columns[0].Visible = false;
    janTbl.Columns[1].Caption = "Part Number";
    janTbl.Columns[1].ShowRowSelector = true;
    janTbl.Columns[1].EditType = Janus.Windows.GridEX.EditType.NoEdit;
    janTbl.Columns[1].Width = 120;

} // <--- and this

没有大括号,只有foreach()语句后面的第一行才是循环的一部分。之后的代码在循环之外,就像你写的那样:

foreach (Janus.Windows.GridEX.GridEXTable janTbl in lvtoolpartsinfo.Tables)
        janTbl.Columns[0].Visible = false;

janTbl.Columns[1].Caption = "Part Number";
janTbl.Columns[1].ShowRowSelector = true;
janTbl.Columns[1].EditType = Janus.Windows.GridEX.EditType.NoEdit;
janTbl.Columns[1].Width = 120;