hi, i have a problem of Index was outside the bounds of the array. i don't understand how to amend the existing solution in my case. anyone could help me? the message error is System.IndexOutOfRangeException {"Index was outside the bounds of the array. below is the code & the error at line 109 (strTextOut = arrMicrosensor(intNum).strCode)
Public Function sensorWithinRange() As MicrosensorStruct()
Dim arrMicrosensor() As MicrosensorStruct '
Dim dblNearestStation As MicrosensorStruct '
Dim intStationCounter As Integer 'Counter for stations
Dim dblStationE As Double 'Easting coords of one station
Dim dblStationN As Double 'Northing coords of one station
Dim intNearestSensor As Integer '
Dim strNearestSensor() As String '
Dim intA, intB, IntMaxIndex As Integer '
Dim dblMaxValue As Double '
Dim sMax, sTemp As MicrosensorStruct '
Dim strTextOut As String '
Dim strArrColumns() As String 'Columns within 1 station record
Dim chrArrDelim() As Char = " ,".ToCharArray() 'File delimiter
Dim sReader As IO.StreamReader
Dim strRecord As String
intStationCounter = -1
intNearestSensor = 0
dblStationE = CDbl(txtEasting.Text)
dblStationN = CDbl(txtNorthing.Text)
sReader = IO.File.OpenText(txtFilename.Text)
strRecord = sReader.ReadLine()
strRecord = sReader.ReadLine()
Do While (Not sReader.EndOfStream)
strRecord = sReader.ReadLine()
intStationCounter = (intStationCounter + 1)
Loop
sReader.Close()
Dim intNrStations As Integer 'Total number of station
ReDim arrMicrosensor(intNrStations)
sReader = IO.File.OpenText(txtFilename.Text)
strRecord = sReader.ReadLine()
dblNearestStation.dblRngeDist = Double.PositiveInfinity
For intStationCounter = 0 To intNrStations - 1
strRecord = sReader.ReadLine()
strArrColumns = strRecord.Split(chrArrDelim)
arrMicrosensor(intStationCounter).lngID = CLng(strArrColumns(0))
arrMicrosensor(intStationCounter).strOUTCORP = strArrColumns(1)
arrMicrosensor(intStationCounter).strCode = strArrColumns(2)
arrMicrosensor(intStationCounter).dblNorthing = CDbl(strArrColumns(3))
arrMicrosensor(intStationCounter).dblEasting = CDbl(strArrColumns(4))
arrMicrosensor(intStationCounter).dblRngeDist = CalculateDistance(dblStationN, dblStationE, arrMicrosensor(intStationCounter).dblNorthing, arrMicrosensor(intStationCounter).dblEasting)
If arrMicrosensor(intStationCounter).dblRngeDist < CDbl(txtRangeDist.Text) Then
intNearestSensor = intNearestSensor + 1
'string for text in listbox
lboNearestStation.Items.Add("STATION ID:" &amp; arrMicrosensor(intStationCounter).lngID.ToString &amp; " , " _
&amp; "REFERENCE CODE:" &amp; arrMicrosensor(intStationCounter).strCode &amp; " , NORTHING:" _
&amp; arrMicrosensor(intStationCounter).dblNorthing.ToString("F") &amp; ", EASTING:" _
&amp; arrMicrosensor(intStationCounter).dblEasting.ToString("F") &amp; ", at:" _
&amp; arrMicrosensor(intStationCounter).dblRngeDist.ToString("F") &amp; "m")
End If
Next intStationCounter
sReader.Close()
ReDim strNearestSensor(intNearestSensor - 1)
For intStationCounter = 0 To intNrStations - 1
If arrMicrosensor(intStationCounter).dblRngeDist < CDbl(txtRangeDist.Text) Then
intNearestSensor = 0
strNearestSensor(intNearestSensor) = arrMicrosensor(intStationCounter).strCode
End If
Next
Dim intNum As Integer
intNum = CInt(txtNrSensor.Text)
'For intNum = 0 To intNum - 1
For intNum = 0 To intNum - 1
For intA = arrMicrosensor.GetUpperBound(0) To 1 Step -1
sMax = arrMicrosensor(0)
dblMaxValue = sMax.dblRngeDist
IntMaxIndex = 0
For intB = 0 To intA
If arrMicrosensor(intB).dblRngeDist &gt; dblMaxValue Then
dblMaxValue = arrMicrosensor(intB).dblRngeDist
IntMaxIndex = intB
End If
Next intB
sTemp = arrMicrosensor(intA)
arrMicrosensor(intA) = arrMicrosensor(IntMaxIndex)
arrMicrosensor(IntMaxIndex) = sTemp
Next intA
strTextOut = arrMicrosensor(intNum).strCode
Next intNum
Return arrMicrosensor
End Function