Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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,
Posted

There is a web site that does code conversion for you: Developer Fusion: Convert VB.NET to C#[^]

This is the result from feeding it what you pasted into your question:

C#
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;
    }
}


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!
 
Share this answer
 
v2
Comments
jaipal0908 11-Jun-12 11:04am    
Thank you,but i am getting error at void after conversion in line
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
at void
VJ Reddy 11-Jun-12 11:53am    
Good link for conversion. 5!
Prasad_Kulkarni 12-Jun-12 5:10am    
Good one Manfred!
Espen Harlinn 12-Jun-12 8:39am    
5'ed!
Manas Bhardwaj 14-Jun-12 4:25am    
Correct +5
The code can be converted by with http://converter.telerik.com/[^] or using the link given in solution 1 by Manfred R. Bihy.

The converted code is as follows:
C#
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;
    }
}

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

C#
Menu1.MenuItemClick += Menu1_MenuItemClick;


[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
C#
foreach (var item in Menu1.Items) {
    item.ImageUrl = e.Item == item ?
                "selectedtab.gif" : "unselectedtab.gif";
}

instead of the following for loop
C#
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.
 
Share this answer
 
v3
Comments
jaipal0908 12-Jun-12 3:16am    
Hi VJ,

In the above converted code i am getting the error like this.
Compiler Error Message: CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string'

Source Error:



Line 21: for (int i = 0; i < Menu1.Items.Count; i++)
Line 22: {
Line 23: if (i == e.Item.Value)---------Here Error coming
Line 24: {
Line 25: Menu1.Items[i].ImageUrl = "selectedtab.gif";
VJ Reddy 13-Jun-12 11:48am    
Please see the updated solution.

Thank you :)
Prasad_Kulkarni 12-Jun-12 5:10am    
Nice one +5!
VJ Reddy 12-Jun-12 5:51am    
Thank you, Prasad :)
Espen Harlinn 12-Jun-12 8:39am    
5'ed!
 
Share this answer
 
Comments
jaipal0908 13-Jun-12 8:51am    
I converted the code from VB to C# like below..

VB Code:
MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value)
Dim i As Integer
'Make the selected menu item reflect the correct imageurl
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


C# code:

MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
for (int 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";
}
}




But still i am getting this error near "i=e.Item.Value"
CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string'

Can anyone help me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900