0

I'm not very familiar with VB.net at the moment and having some trouble converting the following code to C#.

Dim itemList As New ArrayList
Dim strMyitemList(itemList.Count - 1) As String

        For x = 0 To (itemList.Count - 1)
            strMyitemList(x) = itemList(x)
        Next

So far I've got:

ArrayList itemList = new ArrayList();
string[] strMyitemList = new string[itemList.Count -1];

            for (int x = 0; x <= (itemList.Count - 1); x++)
            {
                strMyitemList[x] = itemList(x);
            }

I'm getting the error CS0149 "Method name expected" on "itemList(x)".

Thanks

6
  • Dont use ArrayList anymore, there is no reason Commented Apr 6, 2018 at 8:17
  • If you're not familiar with vb.net, what prevents you from writing working C# code? Commented Apr 6, 2018 at 8:18
  • @user202729 I am required to convert something that already exists to C# from VB.net. However, was trying to understand the VB to convert it. Commented Apr 6, 2018 at 8:21
  • 1
    change strMyitemList[x] = itemList(x); to strMyitemList[x] = itemList[x]; Commented Apr 6, 2018 at 8:23
  • In C# you shouldn't use itemList.Count - 1 when declaring the array. It is only used in VB.NET because it handles arrays differently. Commented Apr 6, 2018 at 8:33

1 Answer 1

5

itemList is an ArrayList which has an indexer. In C# you use them with []:

strMyitemList[x] = (string)itemList[x];

But nowadays there is no reason to use ArrayList. Use a strongly typed List<string>:

strMyitemList[x] = itemList[x];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tim, I noticed I'd added the square brackets to strMyitemList but not itemList (also forgot the cast).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.