I want to convert VB.Net to C# code

VB.NET-

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim lCount As Integer

            For lCount = 1 To 5
                ListView1.Items.Add(lCount.ToString)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim lCount As Integer
            For lCount = 0 To ListView1.Items.Count
                MsgBox(ListView1.Items(lCount).Text)
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

C#

private void Form1_Load(object sender, EventArgs e)
        {
            int lcount;
            for (lcount = 0; lcount <= 5;lcount ++ )
            {
                listView1.Items.Add( lcount.ToString);
            }

        }

I m getting Errors
Error 1 The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.Add(string)' has some invalid arguments

Error 2 Argument '1': cannot convert from 'method group' to 'string'

Recommended Answers

All 3 Replies

You left off the parenthesis: lcount.ToString() should eliminate that error message.

Use parenthesis.

listView1.Items.Add(lcount.ToString());

Your next loop should look like:

for (int lCount = 0; lCount < ListView1.Items.Count; lCount++)
                MessageBox.Show(ListView1.Items[lCount].Text)

to replace:

Dim lCount As Integer
            For lCount = 0 To ListView1.Items.Count
                MsgBox(ListView1.Items(lCount).Text)
            Next

NOTE: Not sure how VB handles declaration of iterator inside the loop ( lCount (maybe you can't), but in C# the variable lCount (as I demonstrated it) will no longer be in scope outside the confines of the loop block (single statement in this case).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.