0

I have a array list

private ArrayList rps = new ArrayList();

now below code is working fine in 2005 but not in visual studio 2008

int min = Convert.ToInt32(rps.Item(0));
int max = Convert.ToInt32(rps.Item(rps.Count - 1));

Error: System.Collections.ArrayList does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?`

3 Answers 3

3

That code wouldn't work in VS 2005 either. Something similar might work in VB, but not in C#. The C# code would be:

int min = Convert.ToInt32(rps[0]);
int max = Convert.ToInt32(rps.Item[rps.Count - 1]);

However, I'd advise you to start using generic collections such as List<T> instead.

Sign up to request clarification or add additional context in comments.

2 Comments

true, I have converted that from VB
Yep, first thing when I saw the code was wonder why the VB.NET was tagged with C#... till I saw the ;.
3

Use an indexer.

int min = Convert.ToInt32(rps[0]);

Also consider using List<T> instead of ArrayList.

Comments

2

You have a syntax error:

rps.Item(0)

Should be:

rps[0]

Note - you really shouldn't be using ArrayList - it predates generics which is what you should be using instead.

Comments

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.