View Full Version : Visual Basic Problem

9th May 2003, 08:45
Hi guys, ive got a friend who is writing a program in VB for a geophysical application. Im a FORTRAN90 and IDL boy myself, so i dont know what the problem is. If anyone can give some help on this it would be greatly appreciated. Thanks. Here it is:

i am writing an auto-correlation program for short dicrete signals.
i am having a problem where i am getting an error saying the subscript is out of range. my code is below with the problem line starting:
sngAns(intLoop) = sngAns(intLoop) + sngAlldata(intCount) * sngAlldata(intCount - intLoop)

Private sngAlldata(1 To 100) As Single, intSamples As Integer
Private Sub butload_Click()
'declare some varibales
Dim sngdata As Single, strtest As String
Dim intFree As Integer
'set some variables
intFree = FreeFile

'set a dialog box allowing the user to search for his/her data
strtest = CommonDialog1.FileName

'read in a file of data
Open strtest For Input As #intFree
intSamples = 1
'print some headings

' set up a loop that runs till the end of file
Do While Not EOF(intFree)
Input #intFree, sngdata
sngAlldata(intSamples) = sngdata
Text1.SelText = Str(sngdata) & vbCrLf
intSamples = intSamples + 1
intSamples = intSamples - 1
Text1.SelText = "no of samples = " & Str(intSamples) & vbCrLf
End Sub
Private Sub butproautocor_click()
Dim intCount As Integer, intLoop As Integer, sngAns(1 To 199) As Single, intCount1 As Integer, intN As Integer, intM As Integer
For intCount1 = 1 To 199
sngAns(intCount1) = 0
Next intCount1
intN = intSamples
intM = intSamples
'no samples of a and b is n and m respectively
'set loopcounter for tau values to increment difference between them
For intLoop = -(intM - 1) To intN - 1
'to increment staring values to multiply
For intCount = 1 To intN
'to multiply then add them up
sngAns(intLoop) = sngAns(intLoop) + sngAlldata(intCount) * sngAlldata(intCount - intLoop)
Next intCount
Text1.SelText = Str(sngAns(intLoop)) & vbTab & Str(intLoop) & vbCrLf
Next intLoop
End Sub

any help or suggestion will be greatly appreciated

9th May 2003, 08:54
Only use VBA, not full VB, but with a problem like this I would set a pause point just before the for loops start, and then step through the code (F8). Then hover the mouse over the line and check that IntCount and IntLoop are what you expect them to be, and also that sngAns(xx) and sngAllData(xx) are what you expect...

I'm no good at just looking at code and saying "that's the problem!" :)

9th May 2003, 09:01
he says:

the sngans(xx) is givving the subscript out of range error so it does not loop through the calculation. thus putting a watch won't help. but the other part of the calculation i.e.
sngAlldata(intCount) * sngAlldata(intCount - intLoop)
seems to be producing the right values. the problem seems to lie in sngAns(xxx)

9th May 2003, 09:08
are sngAns and SngAllData the same variable type? If not you may need to convert sngAllData to SngAns' type.

9th May 2003, 09:16
he says:

both of them are single precision variables and i also have tried making them the same length with no result. the arrays are 1-D arrays.

9th May 2003, 09:20
well your starting value for intLoop in that for loop is <0. that may be your problem? sngAns(-x) is likely not valid I think (should start at either sngAns(0) or sngAns(1) I can't remember with VB)

9th May 2003, 09:23
Originally posted by jonnylane

For intLoop = -(intM - 1) To intN - 1
Next intLoop
End Sub[/code]

I've got the impression that sngAlldata(intCount) is causing the problem (running out of bounds?), and why does he start with a negative value to start the loop?

9th May 2003, 09:42
thanks guys.

he says:

i did not set the boundaries for sngans for negative numbers so when i did it worked. but there are still some problems but since i know what they are i should be alright.
thanks alot

9th May 2003, 09:43
No probs - just helping you work it out really :)

10th May 2003, 20:43
Woah!! VB?
am learning that in Uni rite now..its gaga!~
can someone explain to me how to use functions and subroutines?? :D:D:D