Does anybody know how I can assign a lot of values to an array at once?
in C I would do some thing like
int MyData[] = {1,1,2,3,7}
is there any way I can do something similar in VB?
Printable View
Does anybody know how I can assign a lot of values to an array at once?
in C I would do some thing like
int MyData[] = {1,1,2,3,7}
is there any way I can do something similar in VB?
You can't initiate variables in VB...yet - that comes with version 7.
Toot
you can do something close though ..
dim myString as string
dim myArray
mystring="1,1,2,3,4,5,6,7,8"
myarray=split (mystring,",")
this will populate the array with the string component (devided by the ",").
For variant arrays you can do this:
Code:Dim a as variant
a=array(1,2,3,4,5)
hmmm
I knew about the array function, but I must admit the split function was new to me. The problem is that in both cases I end up with a variant array. This would not be a problem as such if the initial data was all that i wanted in the array, but unfortunately I need to dynamically resize the array.
Can I still do this with variant arrays?
What if I wanted a multi dimensional array, can I still do it this way?
thanks for any help
you can use
redim preserve array (size)
this will resize the array and keep the information
or ...
you can use dictionary objects and than you don't need to define a size, it is also more efficient with memory, where do you take the information from ? (a db ?)
will redim preserve work with variant arrays as well as with arrays of variants?
The info doesn't come from a db, it depends on waht the user has entered while running the app
I think the Dictionary object is exactly what I want though, thanks
I am not sure about the redim with variants,
But the dictionary objects are great, are you o.k with those ?
Àrrays in variant are just the same as regular arrays except that you can use For each to enumerate them, overwrite them with other arrays and anything else, and they are slightly slower. I'm not sure, i don't have vb here to test but i think you can pass a regular array as a variant into a function and then set the array inside the function. You could pass all the items in a paramarray variant.
Just do this:
That will workCode:Dim MyArray() as long
ReDim MyArray(1) as Long 'do this else will not work
ReDim Preserve MyArray(NewSize)
theres a dictionary type in VB? goes to look that up