2

someone knows if it's possible create a fixed size ArrayList? Or I have to use necessarily an array?

I try with this

Dim array As ArrayList

array = New ArrayList(10)

and

array.Capacity = 10 

But I can add more than 10 items, and it doesn 't show me any kind of error how I expected.

Thanks

M.S.
  • 4,283
  • 1
  • 19
  • 42
Rubén Aroca
  • 85
  • 1
  • 1
  • 7

3 Answers3

2

Just use an Array this size will not change unless you explicitly code it to.

Dim myArray(9) As String 'or whatever object you need Integer, etc.

Note that specifying 9 will create 0-9 i.e. 10 items in your array

(ArrayLists are bad in many many ways so don't use them)

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

Capacity of ArrayList tells the maximum number of items ArrayList can currently hold. Capacity will be increased automatically at run time when more items are added in the ArrayList.

For fixed size, use Array as mentioned below:

Dim intArray(9) As Integer 
M.S.
  • 4,283
  • 1
  • 19
  • 42
0

If you are wanting to store different types in your collection you can use;

Dim myArray(5) As Object

If you wanted to read them back as the type you put them in as you would have to convert their type back what they were originally.

I do not recommend this as an approach. If you want to do this then I suggest you create a custom object such as a class or structures that will contain properties for each of the values you wish to set.

mark_h
  • 5,233
  • 4
  • 36
  • 52