vb. 2005 working with arrays of unknown length

Steve36445

Well-known member
Joined
Dec 23, 2007
Messages
58
Programming Experience
10+
Can you help me please?

I want to declare an array of unknown length and then add items to it I would think something like this but it doesn,t
work


VB.NET:
dim my_array() as string 'length unknown

dim could_be_any_length as integer' not known at time of declaration

could_be_any_length = 34' not known at time of declaration

 For index As Integer = 0 To could_be_any_length
my_array(index)=index.tostring
next


I believe the answer may involve the 'new' keyword but I cannot find the syntax.

Thanks,

Steve
 
Arrays are redimensioned with the ReDim keyword, if you also want to preserve the current content you must also use the Preserve keyword. It is also possible to use the shared Array.Resize method which in addition automatically preserves. In most cases you think you need an array using a collection of some sort will be more efficient and give less coding.
 
re. vb. 2005 working with arrays of unknown length

Thanks, but this does not answer the question. Imagine that you

are inputing data from a sequential file or user imput, the

file length could be 10 or 10 000, there will be no way of

knowing when the array is dimensioned, you would have to

dimension the array oversize eg .

VB.NET:
dim my_array(100000) as string

Count the inputs and then redimendion the array eg.

VB.NET:
redim preseve my_array(input_count)'or use array.resize
Not very convenient or elegant! surely there must be a better

way?

Thanks,

Steve

Ps. What did you mean by "using a _collection_ of some sort " could you please show me?
 
VB.NET:
Dim my_array as arraylist

myarray.add("Some Value")

Messagebox.show(myarray(0)) 'would show "Some Value"

Or

VB.NET:
dim my_List as list(of string)

my_list.add("Some Value")

Messagebox.show(my_list(0)) 'would show "Some Value"

I am no 100% sure on my syntax on the second one, but it should be pretty close.
 
Thanks,
I've found it on msdn.

Arraylists seem fo be one dimensional, is this correct.

For some applications I need 2d. Any ideas?

Thanks,
Steve
 
Well, you could always create an arraylist of arraylists. For example:

VB.NET:
dim mainArray as new arraylist()

mainArray.add(new arraylist())
mainArray.add(new arraylist()) 'now you have a two dimentional arraylist

ctype(mainArray(0), ArrayList).Add("some value")

messagebox.show(ctype(mainarray(0),ArrayList)(0).toString())

It gets kind of ugly, though, as you can see, but it would work.
 
If you give some more information for what it is for, maybe we can help you come up with a different approach.
 
Thanks, but this does not answer the question. Imagine that you
Not very convenient or elegant! surely there must be a better
You can use the ReDim statement as many times you want. If you need to add an element to an array you have to redimension it, to do this you have to use the Redim statement. Of course it would be a lot more efficient to bulk redim in batches of say 1000 and keep track of the real usage, that is what all the collection types does for you automatically.

Multidimensional array? If the columns have an identity you're better off collecting class/structure instances (in most cases:)):
VB.NET:
class member
  public name, id, address as string
end class

sub usage()
  dim collec as new list(of member)
  dim m as new member
  m.name= "the name"
  m.id = "the id"
  m.address = "somewhere"
  collec.add(m)
end sub
 
Back
Top