Friend's,
Please help me work's array in VB, how declarations variables array???
Tnk's
Printable View
Friend's,
Please help me work's array in VB, how declarations variables array???
Tnk's
For starters, here is how you declare arrays:
The above statements create empty arrays of the specified types. You can later resize them. If you want to declare the size of the arrays, you can use:Code:
Dim strArray() as string
dim numArray() as integer
dim varArray() as variant
Code:
dim strArray(3) as string 'Array to hold 3 strings
dim numArray(4,5) as integer 'Array to hold 4x5 integers, 4 rows and 5 columns
There are various topics associated with arrays like the Base index, resizing the arrays etc. Either check out MSDN, get hold of a good VB book or come back to the forum for further clarifications.
Arrays speed up the process a bit.
Code:Private Sub Command1_Click()
Dim x(12) As String
x(0) = "M"
x(1) = "a"
x(2) = "t"
x(3) = "t"
x(4) = "h"
x(5) = "e"
x(6) = "w"
x(7) = " "
x(8) = "G"
x(9) = "a"
x(10) = "t"
x(11) = "e"
x(12) = "s"
For I = LBound(x) To UBound(x)
Print x(I)
Next I
End Sub
If you don't know the size of an array on declaring it your declare it as so:
In a bas module
Code:Option Explicit
Public MyArr()
Then later in your code, lets say you are reading a file using line input and then you want to store the lines in an array your would do this:
Option Explicit
'some event on the form
Dim i As Integer, myVar as String
Open ("C:\myfile.txt") for Input as #1
While Not EOF(#1)
ReDim Preserve MyArr(i)
Input #1, myVar
myArr(i) = myVar
i = i + 1
Loop
Close #1
To read the array into a list box
For i = lBound(myArr) to Ubound(myArr)
list1.addditem myArr(i)
next i
hmm, does microsoft produce VB in different countries' languages? ie french or german, instead of using english words such as Select Case... or If-then-else
just a thought.
wossname
How in anyone's name does that tie into Arrays?
:D
bty
Do you know why a chair is called a chair and not a table?
VbWorld.net Has an excellent article about arrays, read it here:
http://www.vb-world.net/articles/arrays/
If you want to make a cycle into the array try this:
dim int_I as integer
dim int_J as integer
dim a() as integer
...
...
...
int_J=6
redim a(int_J)
for int_I=lbound(a) to ubound(a)
...
...
next int_I
By the way, if you´re using the preserve keyword, it can be done on the last dimension of the array.
Matthew,
On reading your reply I thought there was a mistake in the code. You declared the array of string with a dimension of 12. I took it to be the total number of elements in the array. Actually it is not so.
This is another point of the arrays which I had failed to notice. And this is specific to VB.
In all programming languages, when you declare an array you define the size of the array, i.e. the total number of elements in the array. But in VB you declare the Upper Bound of the array, i.e. the index of the last element in the array.
Another interesting point is in this case the Option Base statement decides how many elements your array can hold. For e.g. in Matthew's example, Option Base 0 will store 13 elements in the array (Index 0 to Index 12) while Option Base 1 will store only 12 elements (Index 1 to Index 12).
Are my observations correct?