Your array indexes must be integers
I think the problem is that you aren't using whole numbers for your array indexes in the loops. I have never seen an array use singles as an index, and I don't think you can do that. (I could be wrong...?).
I changed your code using an integer variable for your array index, so now it works just fine (although I can't for the life of me figure out what the purpose is!).
Try this:
Code:
Option Explicit
'This code assumes a text box called Text1 set to multiline
'true and a command button named Command1
Private Sub Command1_Click()
Dim ResultsArray() As Single
Dim g As Single
Dim r As Single
Dim l As Single
Dim h As Single
Dim v As Single
Dim TempStr As String
Dim ArrayIndex As Integer
r = 20
TempStr = ""
ArrayIndex = 1
ReDim ResultsArray(1 To 4 * r)
For h = 1 To (2 * r) Step 0.5
v = h * 2
ResultsArray(ArrayIndex) = v
ArrayIndex = ArrayIndex + 1
Next h
ArrayIndex = 1
For h = 1 To r Step 0.5
TempStr = TempStr & h & Chr(9) & ResultsArray(ArrayIndex) & _
Chr(9) & Chr(9) & Chr(9) & _
h + r & Chr(9) & ResultsArray(ArrayIndex + r * 2) & vbCrLf
ArrayIndex = ArrayIndex + 1
Next h
Text1.Text = TempStr
End Sub
Edited by seaweed on 03-10-2000 at 05:01 AM
Same problem, same solution...
From what I can tell, you are still going to run into the same problem of using a single as an array index.
Now, if the commented code that you last posted returns correct results, then we can infer that if the Step value is 1, then the upper bound of the array is 4 * r from this line of code:
Code:
ReDim ResultsArray(1 To 4 * r)
We also know that if you change the step value to 0.5, that the number of elements in the array will double. If you change the step to 0.25, the number of elements will quadruple. This leads into a formula, 1/StepValue. If the step value is .5 then 1/.5 = 2 (doubling the array size), and if it's .25 then 1/.25 = 4 (quadrupling the array size). Therefore, you can use the following formula to calculate the upper bound of the array:
Code:
UpperBound = (1 / StepValue) * 4 * r
What I did is in the Form_Load, ask the user what the step value will be and hold that value in a global variable (Increment). Then just plug that value into the formula and it should work. Try the following and see if you get the correct results:
Code:
Option Explicit
Private Increment As Single 'This holds the step increment
Private Sub Form_Load()
'Step increment is assigned here. You could put this in a
'command button also, if you wanted to try different
'increments without starting and stoping your project each time
Me.Show
On Error Resume Next 'In case user clicks cancel
Increment = CSng(InputBox("What increment do you want to use?"))
If Increment <= 0 Then
Increment = 1
End If
End Sub
Private Sub Command1_Click()
Dim ResultsArray() As Single
Dim Radius As Single
Dim Length As Single
Dim Height As Single
Dim Volume As Single
Dim TempStr As String
Dim Title As String
Dim ArrayIndex As Integer
Radius = 20
Length = 72
TempStr = ""
ArrayIndex = 1
ReDim ResultsArray(1 To (1 / Increment) * 4 * Radius)
For Height = 1 To (2 * Radius) - 1 Step Increment
Volume = Format(Length * ((Radius ^ 2 * (ArcCos((Radius - Height) / _
Radius))) - (Radius - Height) * (Sqr(2 * (Radius * Height) - _
Height ^ 2))) / 231, "######.##")
ResultsArray(ArrayIndex) = Volume
ArrayIndex = ArrayIndex + 1
Next Height
ArrayIndex = 1
For Height = 1 To Radius Step Increment
TempStr = TempStr & Height & Chr(9) & ResultsArray(ArrayIndex) & _
Chr(9) & Chr(9) & Chr(9) & _
Height + Radius & Chr(9) & ResultsArray(ArrayIndex + ((1 / Increment) _
* Radius)) & vbCrLf
ArrayIndex = ArrayIndex + 1
Next Height
Title = "This is a test"
Text1.Text = Title & vbCrLf & TempStr
End Sub
Function ArcCos(x As Double) As Double
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Function
Let me know if this doesn't work! We'll keep plugging away at it.
~seaweed