本文介绍了vb.net到c#.net的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好,

谁能告诉我如何将代码从vb.net转换为c#.net.有什么工具吗?

谁能告诉我以下代码的C#代码.

Hi Good evening All,

Can anyone tell me how to convert code from vb.net to c#.net.?
Is there any tool?

Can anyone tell me the c# code for the following code.

Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs) Handles Menu1.MenuItemClick
       MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value)
       Dim i As Integer
       For i = 0 To Menu1.Items.Count - 1
           If i = e.Item.Value Then
               Menu1.Items(i).ImageUrl = "selectedtab.gif"
           Else
               Menu1.Items(i).ImageUrl = "unselectedtab.gif"
           End If
       Next
   End Sub

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       If Page.IsPostBack = False Then
           Menu1.Items(0).Selected = True
       End If
   End Sub



谢谢您,



Thank you,

推荐答案

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
    MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
    int i = 0;
    for (i = 0; i <= Menu1.Items.Count - 1; i++) {
        if (i == e.Item.Value) {
            Menu1.Items[i].ImageUrl = "selectedtab.gif";
        } else {
            Menu1.Items(i).ImageUrl = "unselectedtab.gif";
        }
    }
}

protected void Page_Load(object sender, System.EventArgs e)
{
    if (Page.IsPostBack == false) {
        Menu1.Items(0).Selected = true;
    }
}



但是必须进行一些调整.就像通过[]而不是通过()访问索引属性Items一样. Handles关键字在C#中不存在,因此您必须通过编码连接事件处理程序.

干杯!



Some adjustments had to be made though. Like accessing the indexed property Items via [] and not with (). The Handles keyword is not present in C# so you''ll have to hook up the event handlers via coding.

Cheers!


protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
    MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
    for (int i = 0; i < Menu1.Items.Count; i++) {
        if (i == e.Item.Value) {
            Menu1.Items[i].ImageUrl = "selectedtab.gif";
        } else {
            Menu1.Items[i].ImageUrl = "unselectedtab.gif";
        }
    }
}

protected void Page_Load(object sender, System.EventArgs e)
{
    if (Page.IsPostBack == false) {
        Menu1.Items[0].Selected = true;
    }
}


转换后,Menu1.Items(i)转换为Menu1.Items(i) ,这可能是错误的原因,因为使用VB.NET中的()括号访问indexer property Items(i),其中C#使用[]访问indexer property Items[i].

因此,以上代码被纠正. C#不允许Handles关键字,因此必须将这些方法分配给相应的事件,例如


After conversion the Menu1.Items(i) is converted as Menu1.Items(i) which may be the reason for an error, as the indexer property Items(i) is accessed using ( ) parentheses in VB.NET where as in C# the indexer property Items[i] is accessed using [ ].

Accordingly the above code is corrected. The C# does not allow Handles keyword, hence these methods have to be assigned to the corresponding events like

Menu1.MenuItemClick += Menu1_MenuItemClick;



[更新]
解决方案已根据以下OP的评论进行了更新:


错误
编译器错误消息:CS0019:运算符"=="不能应用于类型为"int"和"string"的操作数
在线
如果(i == e.Item.Value)---------这里出现错误
是由于i属于int type and <code>e.Item.Value属于string type的原因,无法比较.

要解决此错误,可以将e.Item.Value 转换为int
使用Convert.Int32(e.Item.Value),然后可以将其进行比较

从代码中可以看出,在引发click 事件的MenuItem for loop ImageUrl属性中,将其设置为selectedtab.gif;对于其他项目,将其设置为unselectedtab.gif,这可以通过使用e.Item本身如下without involving the overhead of conversion of e.Item.Value



[Update]
The Solution is updated in response to the comment of OP below:


The error
Compiler Error Message: CS0019: Operator ''=='' cannot be applied to operands of type ''int'' and ''string''
at the line
if (i == e.Item.Value)---------Here Error coming
is because of the reason that i is of int type and <code>e.Item.Value is string type which cannot be compared.

To resolve this error e.Item.Value can be converted to int
using Convert.Int32(e.Item.Value) and then it can be compared

As seen from the code, in the for loop ImageUrl property of the MenuItem which raised the click event is set to selectedtab.gif and for the other items it is set to unselectedtab.gif, which can be achieved by using the e.Item itself as follows without involving the overhead of conversion of e.Item.Value

foreach (var item in Menu1.Items) {
    item.ImageUrl = e.Item == item ?
                "selectedtab.gif" : "unselectedtab.gif";
}


而不是以下for循环


instead of the following for loop

for (int i = 0; i < Menu1.Items.Count; i++) {
   if (i == e.Item.Value) {
       Menu1.Items[i].ImageUrl = "selectedtab.gif";
   } else {
       Menu1.Items[i].ImageUrl = "unselectedtab.gif";
   }
}


在上面的代码中.


in the above code.



这篇关于vb.net到c#.net的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:35