Arrays

A

AA2e72E

How do I initialise an string array of rank 2 whose first dimension is
unknown at the time of initialisation? I know the second dimension is 3.

I have tried several variations in code and I end up with either an array
whose dimensions are fixed or with the error 'Use of unassigned local
variable ..'
Thanks.
 
A

Alberto Poblacion

AA2e72E said:
How do I initialise an string array of rank 2 whose first dimension is
unknown at the time of initialisation? I know the second dimension is 3.

I have tried several variations in code and I end up with either an array
whose dimensions are fixed or with the error 'Use of unassigned local
variable ..'

You can't do that with an array; all the dimensions need to be known at
the time of initialisation. You can use some other type of container, such
as a List<string[]>. You can then Add a variable number of
single-dimensional string arrays to the List.
 
C

Ciaran O''Donnell

You could always wait to initialise the array until you know. Or as was said
use the List<string[]> to build it. Then call ToArray() to get as string[][].
Jagged arrays (arrays of arrays like [][]) are normally faster to use as the
pointer control is more optimized.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Alberto Poblacion said:
AA2e72E said:
How do I initialise an string array of rank 2 whose first dimension is
unknown at the time of initialisation? I know the second dimension is 3.

I have tried several variations in code and I end up with either an array
whose dimensions are fixed or with the error 'Use of unassigned local
variable ..'

You can't do that with an array; all the dimensions need to be known at
the time of initialisation. You can use some other type of container, such
as a List<string[]>. You can then Add a variable number of
single-dimensional string arrays to the List.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top