Could somebody please give me an example of how to access the contents of an array?
I have the following:
someNumbers(12345, 56456, 123432, 12324)
I would like to use this array in a loop and iterate through it, but when I try to access an individual index I get an error.
For example:
For counter = 1 To 4
Consol.Write(someNumbers(counter))
Next counter
Thanks.
Also, is it possible to populate an array one index at a time while iterating a loop?
array example
Re: array example
Here is code for accessing a 1-dimension array:hacktorious wrote:Could somebody please give me an example of how to access the contents of an array?
Code: Select all
Dim data(1 to 20) as Byte
Sub Main()
Dim i as Byte
' populate the array
For i = 1 to 20
data(i) = i * 10 + 5
Next i
' display the array contents
For i = 1 to 20
Debug.Print "data("; CStr(i); ") = "; CStr(data(i))
Next i
End Sub
This idea can be extended to a multi-dimension array by adding additional indices to the definition and the accesses.
- Don Kinzer
Re: array example
I'm not quite sure what you meant by this. Did you intend to define a 4-dimension array or a 1-dimension array with 4 elements? I suspect that your intention was the latter and you also wanted to initialize the array with the 4 values shown. For a RAM-based array, you'd have to write code to set the four values, e.g.hacktorious wrote:I have the following:
someNumbers(12345, 56456, 123432, 12324)
Code: Select all
Dim someNumbers(1 to 4) as Long
someNumbers(1) = 12345
someNumbers(2) = 56456
someNumbers(3) = 123432
someNumbers(4) = 12324
Code: Select all
Dim counter as Integer
For counter = 1 To 4
Console.Write(CStr(someNumbers(counter)))
Next counter
If the data values never change, you can use a Program Memory data item and provide compile-time initialization for it like this:
Code: Select all
Dim someNumbers as LongVectorData ({12345, 56456, 123432, 12324})
Sub Main()
Dim counter as Byte
For counter = 1 to CByte(UBound(someNumbers))
Console.Write(CStr(someNumbers(counter)))
Next counter
End Sub
Last edited by dkinzer on 19 March 2007, 19:34 PM, edited 1 time in total.
- Don Kinzer
-
- Posts: 15
- Joined: 22 February 2007, 12:05 PM