0

我发现以下对我有用的 C# 代码:

public partial class Default2 : System.Web.UI.Page 
  { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (Session["EID"] != null) 
        { 
            int EmpID = (int)Session["EID"]; 

            DataClassesDataContext dc = new DataClassesDataContext(); 
            var empInfo = from emp in dc.EmployeeLogins 
                          where emp.EmployeeID == EmpID 
                          select new
                          { 
                                     emp.EmployeeLoginKey, 
                                     emp.EmployeeID, 
                                     emp.username, 
                                     emp.passwd 
                          }; 

           foreach (var v in empInfo) 
           { 
               lblID.Text = v.EmployeeID.ToString(); 
               lblLoginKey.Text = v.EmployeeLoginKey.ToString(); 
               lblPassword.Text = v.passwd.ToString(); 
               lblUserName.Text = v.username.ToString(); 

            } 
       } 
       else 
       { 
           Response.Redirect("Default.aspx"); 
        } 
    } 
} 

之前用在线转换器转成vb,编译程序的时候报错如下一句:

For Each v As var In empInfo

如何将var从c#转换为vb?

4

4 回答 4

2

只需跳过类型声明:For Each v In empInfo.

你必须Option Infer On设置。

正式的For Each语句语法在 MSDN 上描述如下:

For Each element [ As datatype ] In group
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ element ]
于 2013-03-15T07:44:00.113 回答
0

试试这个代码:

For Each Dim item In empInfo
于 2013-03-15T07:43:37.133 回答
0
For Each v In empInfo

要让 VB 推断变量类型,只需省略As子句,例如

Dim a = b + c

注意:这仅在Option Inferis时有效On。当Option Inferis时Off,没有类型说明符的变量将默认为Object

于 2013-03-15T07:48:41.487 回答
0

尝试

foreach (Dim v in empInfo) 
                lblID.Text = v.EmployeeID.ToString()
                lblLoginKey.Text = v.EmployeeLoginKey.ToString()
                lblPassword.Text = v.passwd.ToString()
                lblUserName.Text = v.username.ToString()
 Next

不要忘记在页面顶部添加以下行

Option Strict On
Option Infer On
于 2013-03-15T07:56:12.377 回答