Computing.Net > Forums > Programming > VB Array Trouble

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB Array Trouble

Reply to Message Icon

Original Message
Name: edga_dave
Date: November 30, 2004 at 09:02:59 Pacific
Subject: VB Array Trouble
OS: xp pro
CPU/Ram: pentium 4 - 265
Comment:

I have an array which stores the values of PH in water samples.

Codes: Button One
Public PHARRAY(100) as single


Dim i, j As Integer
j = 1
i = 1

For i = 0 To 100
If PHARRAY(Count) > 0 Then
j = j + 1
End If
Next

PHARRAY(j) = Val(newphreading.Text)


Code : Button 2

Dim k As Integer
k = 0

For k = 0 To j + 1
MsgBox PHARRAY(k)
Next

end

The problem i have is that after i enter the data via button one and a txt box, i what 2 test that its stored properly, hence button 2, BUT, say i enter data such as "1" "2" "3" via button one and the txt box and click button 2 i get the result "0" "3"

any ideas?

Sorry about the long post


Report Offensive Message For Removal


Response Number 1
Name: kaepeekay
Date: November 30, 2004 at 20:10:18 Pacific
Reply: (edit)

For i = 0 To 100
If PHARRAY(Count) > 0 Then
j = j + 1
End If
Next

I suspect the above loop. You used PHARAY(count) instead of PHARRAY(i). count will always be zero. Hence J will always be 1. Whatever you enter as the last value will always be the second element of array.ie.index is 1. 0 index will be zero because default value of single is 0 as array is of type single.


Report Offensive Follow Up For Removal

Response Number 2
Name: edga_dave
Date: December 1, 2004 at 02:55:01 Pacific
Reply: (edit)

Thanks for the reply.

I changed the code and it work... well kind of

code changed:

For i = 0 To 100
If PHARRAY(i) > 0 Then
j = j + 1
End If
Next

If i enter "1" "2" "3" it will now display only "1" "2"!! It will only show the first two entires.

the code to display the data is:

Private Sub phstatistics_Click()
Dim k As Integer

For k = 0 To j + 1
MsgBox PHARRAY(k)
Next
End Sub

any ideas?


Report Offensive Follow Up For Removal

Response Number 3
Name: kaepeekay
Date: December 1, 2004 at 04:25:49 Pacific
Reply: (edit)

I have changed the code entirely from what you have been expecting. Just go through.

In your showing procedure, j-I think you are declaring it locally(if not declared means it is variant). When you are using that in For Loop it gets the integer value of 0 and j+1 will always be 1 and only 0 and 1 index values will be shown.

The rewritten code is

Private PHARRAY(100) As Single
Dim i, j As Integer
Private Sub Command1_Click()
For i = 0 To 100
If PHARRAY(i) <= 0 Then
j = i
PHARRAY(j) = Val(Text1.Text)
Exit For
End If

Next
End Sub
Private Sub Command2_Click()
Dim k As Integer
For k = 0 To j
MsgBox PHARRAY(k)
Next
End Sub


Paste that in a form and check. This is what I inferred


Report Offensive Follow Up For Removal

Response Number 4
Name: edga_dave
Date: December 1, 2004 at 09:18:44 Pacific
Reply: (edit)

Thanks again for the reply, with a little tweaking here and there it works fine...

altho i do have another problem...

I wish to find the average data from the entered data EG...

Data entered:
"1" "2" "3" "4" "5"

Calculation for finding the average:

(1+2+3+4+5) / 5

how do i do this when the data is in an array?

I dont want too show the working, i just want the result to appear in a lable.

just incase it helps, new code is;

Public j, k as single

Data Input Code:

PHARRAY(j) = Val(newphreading.Text)

Waterinformation.currentph.Caption = PHARRAY(j)

If Val(Waterinformation.currentph.Caption) >= 6.4 _
And Val(Waterinformation.currentph.Caption) <= 7.6 Then
Waterinformation.phcolourgoodbad.BackColor = vbGreen
Else
Waterinformation.phcolourgoodbad.BackColor = vbRed
End If

Waterinformation.Enabled = True
j = j + 1
Unload Me

Data Output Code:

For k = 0 To j - 1
MsgBox PHARRAY(k)
Next k


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software