1) Lets say you need to draw a graphs for the functions y=x^2+3*x+c; y=-x^2+3*x+c; y=-x^7+5*x+4*c (Note: This function is continuous hyperbole, so you do not need to analyze it). Below you can find a vector- and matrix array solutions.
CODE
Sub calculate_vector(x, c%, nr%)
ReDim y_1%(nr), y_2%(nr), y_3%(nr), x_1$(nr)
For i = 0 To nr
y_1(i) = x ^ 2 + 3 * x + c
y_2(i) = -x ^ 2 + 3 * x + c
y_3(i) = x ^ 7 + 5 * x + 4 * c
x_1(i) = x
x = Round(x - 0.2, 4)
Next i
Call drawgraph(y_1, y_2, y_3, x_1)
End Sub
Sub calculate_matrix(x, c%, nr%)
ReDim y(3, nr)
For i = 0 To nr
y(1, i) = x ^ 2 + 3 * x + c
y(2, i) = -x ^ 2 + 3 * x + c
y(3, i) = x ^ 7 + 5 * x + 4 * c
y(0, i) = x
x = Round(x - 0.2, 4)
Next i
Call drawgraph(y)
End Sub
Some people would wonder what would be the difference of the direct output. Well most important things are that:
1) code would need to be structured (parts could be used by itself)
2) retrieving values takes less time then to valuate them each time you need them
3) in some cases multi-value con referral is a lot faster
Examples before are not structured rightly, so it should be something like
CODE
Sub main()
Const nr = 8
ReDim y(3, nr)
Call calculate_matrix(x, c, y)
Call drawgraph(y)
End Sub
Sub calculate_matrix(ByVal x, ByVal c%, ByRef y())
For i = 0 To UBound(y, 2)
y(1, i) = x ^ 2 + 3 * x + c
y(2, i) = -x ^ 2 + 3 * x + c
y(3, i) = x ^ 7 + 5 * x + 4 * c
y(0, i) = x
x = Round(x - 0.2, 4)
Next i
End Sub
Const nr = 8
ReDim y(3, nr)
Call calculate_matrix(x, c, y)
Call drawgraph(y)
End Sub
Sub calculate_matrix(ByVal x, ByVal c%, ByRef y())
For i = 0 To UBound(y, 2)
y(1, i) = x ^ 2 + 3 * x + c
y(2, i) = -x ^ 2 + 3 * x + c
y(3, i) = x ^ 7 + 5 * x + 4 * c
y(0, i) = x
x = Round(x - 0.2, 4)
Next i
End Sub
To explain what multi-value con referral means, i would use a help of VBA. (To increase the effect the dimensions are switched.) It simply means, that we give all the values at once, so we do not turn to object each time that we have a value to give. This is a lot faster, because turning to the object takes time.
CODE
Sub main()
Const nr = 1000
ReDim y!(nr, 3)
Call calculate_matrix(-1, 4, 0.002, y)
Call drawgraph(y)
End Sub
Sub calculate_matrix(ByVal x, ByVal c!, ByVal s!, ByRef y!())
For i = 0 To UBound(y, 1)
y(i, 1) = x ^ 2 + 3 * x + c
y(i, 2) = -x ^ 2 + 3 * x + c
y(i, 3) = x ^ 7 + 5 * x + 4 * c
y(i, 0) = x
x = Round(x + s, 7)
Next i
End Sub
Sub drawgraph(y!())
Range("A1:" & Chr(96 + UBound(y, 2) - LBound(y, 2)) & UBound(y, 1) - LBound(y, 1) + 1) = y()
End Sub
Const nr = 1000
ReDim y!(nr, 3)
Call calculate_matrix(-1, 4, 0.002, y)
Call drawgraph(y)
End Sub
Sub calculate_matrix(ByVal x, ByVal c!, ByVal s!, ByRef y!())
For i = 0 To UBound(y, 1)
y(i, 1) = x ^ 2 + 3 * x + c
y(i, 2) = -x ^ 2 + 3 * x + c
y(i, 3) = x ^ 7 + 5 * x + 4 * c
y(i, 0) = x
x = Round(x + s, 7)
Next i
End Sub
Sub drawgraph(y!())
Range("A1:" & Chr(96 + UBound(y, 2) - LBound(y, 2)) & UBound(y, 1) - LBound(y, 1) + 1) = y()
End Sub
2) There are a lot of way to valuate a array, but by far 1 best way, that is implemented in VB, is the split() function. We can split a string to 1-dimensional array values.
CODE
a_s = "as,df,gh,hj,kl,צה"
a_m = Split(a_s, ",")
would be about same as to write:
CODE
a_m(0) = "as"
a_m(1) = "df"
a_m(2) = "gh"
a_m(3) = "hj"
a_m(4) = "kl"
a_m(5) = "צה"
Note: In vb 9, it is supported to retrieve values from array as it would be a database, this is WoW because its not some fancy extra, but implemented support to the language! So if you would want to make a new array, from existing arrays every 4'th element, you would be able to do that with 1 line of code. (powered by linq)
3) Sometimes you need to increase the size of an array and keep the data
VB Classic
ReDim Preserve a(ubaund(a))
VB 8
a) ReDim Preserve a(a.GetLength(0))
B) Array.Resize(a, a.GetLength(0) + 1)
If you want to format the data, just lose the optional Preserve command.