[英]How to convert Var from C# to VB?
I found the following C# code which is useful for me: 我找到了以下对我有用的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");
}
}
}
I had used online converter converted to vb, when I compile the program, it returns an error for the following sentence: 我曾使用转换为vb的在线转换器,当我编译程序时,它返回以下句子的错误:
For Each v As var In empInfo
how to convert var from c# to vb? 如何将var从c#转换为vb?
Just skip type declaration: For Each v In empInfo
. 只需跳过类型声明:
For Each v In empInfo
。
You have to have Option Infer On
set. 您必须设置
Option Infer On
。
Formal For Each
statement syntax is described on MSDN as following: Formal
For Each
语句语法在MSDN上描述如下:
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
尝试使用此代码:
For Each Dim item In empInfo
For Each v In empInfo
To get VB to infer variable types, just omit the As
clause, eg 要让VB推断变量类型,只需省略
As
子句,例如
Dim a = b + c
NOTE: This will only work when Option Infer
is On
. 注意:这仅在
Option Infer
为On
。 When Option Infer
is Off
, then variables without type specifiers will default to Object
instead. 当
Option Infer
为Off
,没有类型说明符的变量将默认为Object
。
Try with 试试吧
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
Don't forget to add below lines at top of the page 不要忘记在页面顶部添加以下行
Option Strict On
Option Infer On
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.