Problems with list

icycold68

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

I have a function that is supposed to add values to a list if a certain condition is true. You will see from my code that the result returned by a function is checked to see if it is less then 30. This value represents a distance expressed as miles. If it is less than 30 then it will add the associated postcode pulled from the database to the list. What seems to be happening at present, however, is that postcodes for values greater than 30 are being added to the list and I have no idea why. Could someone please tell me what I'm doing wrong?
Do While myReader2.Read()
				
			lat2 = myReader2("lat").ToString() 
			lon2 = myReader2("lng").ToString() 
			
			unit = "M"


			Dim return_distance As String = distance(lat1, lon1, lat2, lon2, unit)
		
			If return_distance.length <> 1 Then
		
				trimmed_distance = return_distance.SubString(0,4)


			Else
		
				trimmed_distance = return_distance
			
			End If
			
			Dim converted_distance As Integer
			
			converted_distance = CINT(trimmed_distance)
		
			If converted_distance <= 30.00 Then
  				postcode_match.Add(myReader2("outcode").ToString())
			End If
			
		Loop	
			
		Dim find_postcodes() As String = postcode_match.ToArray


		myReader2.Close()
 
Last edited by a moderator:
I'll say one thing first: it makes no sense to declare 'converted_distance' as type Integer and then compare it to a Double. What's this for:
VB.NET:
If converted_distance <= 30[COLOR="#FF0000"].00[/COLOR] Then
It won't hurt but it doesn't make sense and shows a lack of understanding of numeric types. An Integer can contain whole numbers only, so the decimal part is pointless.

As for the question, that If statement will not catch data for which 'converted_distance' is greater than 30 so if you're getting values in the List that you shouldn't then that means that 'converted_distance' must contain the wrong value. You need to investigate why.

Speaking of not understanding numeric types, the fact that latitude, longitude and distance are all numeric values and you're using Strings for them is further evidence that that is the case. Mathematics is performed on numbers so why are theer any Strings involved at all? Everything should be numbers from go to whoa.
 
You are correct in what you say, I have no idea why I didn't notice this sooner but have now changed all types to Double variable types. I do, however, still get the same problem.
 
We have no magic I'm afraid. The code you have shown looks basically correct, although we can only guess at what this is all about:
VB.NET:
            If return_distance.length <> 1 Then
         
                trimmed_distance = return_distance.SubString(0,4)
 
 
            Else
         
                trimmed_distance = return_distance
             
            End If
given that you have provided no explanation and your code is completely devoid of comments. How is anyone who doesn't already know supposed to work out what's going on there? We can see what the code is doing but there's absolutely no indication of WHY it's doing it, which is the important thing. If we know what the actual purpose is then we can determine whether the code actually fulfils the purpose. As it stands, as far as we're concerned, that code is completely arbitrary.

That said, my guess would be that you 'distance' method is producing incorrect results. Again, we have no idea what it does so we can only speculate. That should be the first place you look because that is directly responsible for the value of 'converted_dictance'. Start by ignoring your code and picking up a pen and paper and writing down the steps that the calculation requires. Once you have an algorithm that works when you do manual calculations, then you have something you can actually compare your code to to see if it does what it's supposed to.
 
Back
Top