Library tutorials & articles
Arrays
Declaring Arrays
All arrays in VB have to be declared. The declaration tells VB the name of the array, how many items it can store, and what sort of data it can store. If you declare the array in the general declarations section of a form, the array is visible to all procedures and functions in that form. If you declare the array as Public in a Module it will be visible to every procedure and function in your project. To declare an array use the following syntax:
Dim ArrayName(LowerValue To HigherValue) As DataType
Dim specifies the scope of the array. If you declare it in a Form, use Dim or Private, if you declare it in a Module and you want every procedure to access it, declare it as Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. LowerValue is the first element in the array. HigherValue is the last element in the array. DataType is a valid datatype (ie String)/ You don't have to specify the LowerValue, but if you don't, the array will start from 0. For example,
Dim sTestArray(0 To 10) As String
is the same as
Dim sTestArray(10) As String
The code below declares nArray, with 1 as the first element, 30 as the last element, all of which stores Integers.
Dim nArray( 1 To 30 ) As Integer
Related articles
Related discussion
-
Run-time error '91'
by crazyidane (0 replies)
-
Problem handling Redirects with MSXML2.XMLHTTP
by brandoncampbell (2 replies)
-
vbinputbox pauses code while it waits on response. How can I reproduce that?
by brandoncampbell (1 replies)
-
Sending SMS in VB 6
by sirobnole (6 replies)
-
Comboxbox listindex in ActiveX Control
by brandoncampbell (1 replies)
I'm a bit confuse. I would like user to insert how many data they want to put in the array.
When I declare the array like this
Dim subjArr( ) As DataType
How should I write the code so array size is according to user input. This is the code for user to insert how many records should be inserted.
Dim SubArr() As DataType
Dim subjNo As String
subjNo = InputBox("Enter the no of subjects to calculate: ")
MsgBox (subjNo + " subjects will be calculated")
It would work if you used Dim instead of Const. A constant array is not possible in VB and I really don't see the point. Just use a normal array and make sure that it is not modified.
Thanks James
This is a good Array Tutorial for VB Newbies like me.
These guys have a perfect working example. The randomizer isn't truly random but completely adequate for basic purposes. I couldn't get it to work as a function, but it worked great when I simply used to function code in-line, so I know it works.
http://www.4guysfromrolla.com/webtech/110800-1.shtml
I really just added this in case anyone comes across this in a search as I did
+++++++++++++++
check it: FLEETING IMAGE
How do I make a constant array? When I try "const data(1 to 72) as byte" it doesn't work, where it would if I used Dim rather than const. Also, isn't there some way to set a value to the whole thing? Like with one command making data(1) = 4, data(2) = 7 etc.
No, I believe he means something like this:
Say you had a string array with 6 elements, beginning with zero.
The values were arranged like this:
strArray(0) = "Step 1"
strArray(1) = "Step 2"
strArray(2) = "Step 3"
strArray(3) = "Step 4"
strArray(4) = "Step 5"
strArray(5) = "Step 6"
He wants to randomly arrange the elements' contents, like this:
strArray(0) = "Step 4"
strArray(1) = "Step 3"
strArray(2) = "Step 5"
strArray(3) = "Step 1"
strArray(4) = "Step 6"
strArray(5) = "Step 2"
I hope this is correct, and/or clear.
Are you speaking of randomly pulling one of the values as in say a random name generator?
Can anyone tell me if it's possible to randomly sort the elements in an array?
I'm not familiar to 3d arrays, but could you use something similar to:
I use that to copy 1d arrays.
Not the best piece of code but meh.
Does anyone know a way of transferring the content of an array into another array WITHOUT doing this?:
Public Sub MoveArray()
For Z = 0 To 10
Array2(Z) = Array1(Z)
Next Z
End Sub
I am dealing with some very large (3 dimensional) arrays and the above kind of operation seems a little slow for the user to sit around waiting for. (Especially when it will need to be used in the Undo Function)
Any suggestions will be gratefully received.
couling@fsnet.co.uk">pcouling@fsnet.co.uk
This thread is for discussions of Arrays.