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))
 
Sorry Ian, should have explained that I am storing the results of the list in an array using the code: Dim find_postcodes() As String = postcode_match.ToArray.
 
Hi,

This should answer your question:-

VB.NET:
'this will get the last postcode element in the list
MsgBox("Last Postcode Element is " & postcode_match(postcode_match.Count - 1))
 
'this will iterate the list with a counter instead of using for each
For Counter As Integer = 0 To postcode_match.Count - 1
  'Do whatever you want with the with the postcode here
  MsgBox(postcode_match(Counter))
Next

Just picked up your final post and you need to re-read jmcilhinney's note that he posted earlier on in the thread. You are duplicating your work for nothing and I would advise that you take jmcilhinney's advice one way or the other.

Cheers,

Ian
 
To explain in a little more detail what I am trying to achieve, basically I need to run certain commands if the first element of the array is true and if the last element is true. For example the code I am using is shown below (which isn't working - I'm useless I know!)

If find_postcodes(0) = True Then //If it is the first element of the array

strSQL = strSQL & "AND (Postcode LIKE '%" & find_postcodes(i) & "%'"

ElseIf find_postcodes(i) = find_postcodes.Length - 1 Then //If it is the last element of the array

strSQL = strSQL & "OR Postcode LIKE '%" & find_postcodes(i) & "%')"


Else

strSQL = strSQL & "OR Postcode LIKE '%" & find_postcodes(i) & "%'"


End If
 
Hi,

Right, lets just stop trying to program for a moment. Its not working!

Up until this point you have been saving POSTCODES in either a list or an array, not really sure which at the moment, but where did you get checking for Boolean values would be appropriate? The values that you are saving at the moment are Strings and not Boolean values so your logic is never going to work.

I would suggest that you write down on paper an English written version of what you are trying to do. Forget programming terms for the moment, just use the English language to explain what you are logically trying to achieve in your statements. Read it time and time again until it makes perfect sense to you and then post that English script to the forum so that we can then better advise you where you need to go with a coded version of your logic.

Good luck and cheers,

Ian
 
Back
Top