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'

Dani AI

Generated

Short answer: the compile errors come from two separate issues — a missing pair of parentheses when calling a method in C#, and an off‑by‑one / indexing mismatch when converting VB's inclusive For...To to C#. and correctly called out the ToString call and the loop boundary; the points below expand on those and give safer patterns.

In C# every method call needs parentheses. Writing a method name without () is treated as a method group (not a string), which triggers the “cannot convert from method group to string” error and makes the Add overload fail. You can either call a conversion method (for example Convert.ToString(value)) or construct a ListViewItem explicitly and pass that to the Items collection.

Indexing: VB’s For x = 0 To ListView.Items.Count is inclusive and will attempt to access the index equal to Count (out of range). In C# prefer a half-open loop (i < listView.Items.Count) or, even better, an iteration that avoids index arithmetic entirely:

foreach (System.Windows.Forms.ListViewItem it in listView1.Items)
{
    System.Diagnostics.Debug.WriteLine(it.Text);
}

If you need to add multiple items at once, create ListViewItem objects and use AddRange to reduce per-item overhead:

var arr = new[] {
    new System.Windows.Forms.ListViewItem("One"),
    new System.Windows.Forms.ListViewItem("Two")
};
listView1.Items.AddRange(arr);

Last checks: make sure the code runs after InitializeComponent (Form_Load is fine), the ListView reference isn’t null, and the control’s View and Columns are set appropriately if you expect subitems. Also note C# loop variables declared inside the for statement are scoped to the loop (a behaviour mentioned).

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.