Storing values in an array

icycold68

Active member
Joined
Oct 16, 2012
Messages
30
Programming Experience
3-5
Hello,

I am trying to store the results of a list into an array, but for some reason the array is only storing one value, rather than the full set of values. The code I am using is shown below. Could someone please tell me where I am going wrong?


Dim find_postcode() As String


Dim resultsList As New List(Of String)

For Each dr As DataRow In ds2.Tables(0).Rows
resultsList.Add(dr("PostCode").ToString)
Next


For Each s As String In resultsList

find_postcode(5000000) = (s)


Next

Response.Write(find_postcode(5000000))
 
Hi,

You are assigning the SAME array element in find_postcode in your for loop for each string value that is returned from your ResultsList being :-

find_postcode(5000000) = (s)

That said, it's bit strange that you are assigning each string value to the array element 5,000,000? I guess I know what you want to do but please explain in detail for clarification.

Cheers,

Ian
 
Thanks for the reply Ian.

What I am want to do is to store each value in the List element to an array so that I can filter through all the results in the array at a later stage in the function.
 
Why create a List and then an array? Just create one or the other. You don't need both.
Dim rows As DataRowCollection = ds2.Tables(0).Rows
Dim upperBound As Integer = rows.Count - 1
Dim postcodes(upperBound) As String

For i As Integer = 0 To upperBound
    postcodes(i) = rows(i)("PostCode").ToString()
Next
 
Thank you for your assistance in trying to solve this issue.

I would really appreciate your help on one last matter please.

Basically, I want to loop through a data set and pass through values (latitude and longitude) of each record to a function that will calculate a distance based on the values based to it and then return this distance. If the distance calculated is less than 60 in each case, I want to store the postcode that relates to that latitude and longitude in an array. The code I have used is shown below; however, I have a feeling that this is not the correct (or most efficient) way of doing it. Just for reference, the values for lat1 and lon1 are being passed to the function prior to the section of code shown below.

Dim x As Integer

Do While myReader3.Read()

lat2 = myReader3("lat").ToString()
lon2 = myReader3("lng").ToString()

unit = "M"

Call distance(lat1, lon1, lat2, lon2, unit)

return_distance(5000000) = distance(lat1, lon1, lat2, lon2, unit)

If return_distance(5000000) < 60 Then

postcode_match(5000000) = myReader3("postcode").ToString()

End If

x += 1


Loop

Please help!
 
Hi,

Your use of arrays is terrible. You are also making unnecessary calls to your distance function. If you must use arrays then try this:-

VB.NET:
Define Dim FoundItems As Integer and Dim postcode_match() As String outside the reader loop

lat2 = myReader3.Item("lat")
lon2 = myReader3.Item("lng")
Dim return_distance As Integer = distance(lat1, lon1, lat2, lon2, unit)
 
If return_distance < 60 Then
  ReDim Preserve postcode_match(FoundItems)
  postcode_match(FoundItems) = myReader3.Item("postcode").ToString()
  FoundItems += 1
End If
x += 1
I would however use a list to store your postcodes. Have a look at this:-

VB.NET:
Define Dim postcode_match As List(Of String) outside the reader loop
lat2 = myReader3.Item("lat")
lon2 = myReader3.Item("lng")
Dim return_distance As Integer = distance(lat1, lon1, lat2, lon2, unit)
If return_distance < 60 Then
  postcode_match.Add(myReader3.Item("postcode").ToString())
End If
x += 1
Hope that helps.

Cheers,

Ian
 
Last edited:
Thank you Ian. I agree my use of arrays is pretty bad, however, I am still a novice so am still very much learning as I go along.
 
Hello again,

After using the suggested code as shown below, I now get the following error that keeps occurring. Any ideas why I get this error please?

Object reference not set to an instance of an object.

Code:

Do While myReader3.Read()


lat2 = myReader3.Item("lat")
lon2 = myReader3.Item("lng")

unit = "M"


Dim return_distance As String = distance(lat1, lon1, lat2, lon2, unit)


Dim trimmed_distance As String

If return_distance.length <> 1 Then

trimmed_distance = return_distance.SubString(0,4)


Else

trimmed_distance = return_distance

End If

If trimmed_distance < 60.1 Then
postcode_match.Add(myReader3.Item("outcode").ToString())
End If

Loop



Hi,

Your use of arrays is terrible. You are also making unnecessary calls to your distance function. If you must use arrays then try this:-

VB.NET:
Define Dim FoundItems As Integer and Dim postcode_match() As String outside the reader loop

lat2 = myReader3.Item("lat")
lon2 = myReader3.Item("lng")
Dim return_distance As Integer = distance(lat1, lon1, lat2, lon2, unit)
 
If return_distance < 60 Then
  ReDim Preserve postcode_match(FoundItems)
  postcode_match(FoundItems) = myReader3.Item("postcode").ToString()
  FoundItems += 1
End If
x += 1
I would however use a list to store your postcodes. Have a look at this:-

VB.NET:
Define Dim postcode_match As List(Of String) outside the reader loop
lat2 = myReader3.Item("lat")
lon2 = myReader3.Item("lng")
Dim return_distance As Integer = distance(lat1, lon1, lat2, lon2, unit)
If return_distance < 60 Then
  postcode_match.Add(myReader3.Item("postcode").ToString())
End If
x += 1
Hope that helps.

Cheers,

Ian
 
Hi,

You need to help me further with this one. Exactly where does the code drop out and give this error? Which line of code is this reported on?

I need to go out for a while now so will try and pick up again later.

Cheers,

Ian
 
Sorry, should have thought to mention that. The code errors at the following line:

postcode_match.Add(myReader3.Item("outcode").ToString())


Hi,

You need to help me further with this one. Exactly where does the code drop out and give this error? Which line of code is this reported on?

I need to go out for a while now so will try and pick up again later.

Cheers,

Ian
 
Thank you once again Ian, that works fine :)

I assume to access each element of the array, I just use find_postcodes(i)?

Hi,

It's my own fault for not checking my own code first. The list declaration needs to be Dim postcode_match As New List(Of String).

All should be fine now.

Cheers,

Ian
 
Hi,

No, lists works differently to arrays but are just as easy to work with. To iterate through the list use:-

VB.NET:
For Each strPostcode As String In [I]postcode_match[/I]
  'Do whatever you want with the with the postcode here
  MsgBox(strPostcode)
Next
Not sure where you got find_postcodes from but I am sure you know where to go from here.

Cheers,

Ian
 
Back
Top