Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27419

using selectItems for CalcDistance

$
0
0
I posted on this before and someone helped me but I just could not get it to work. I have been working on it but I have some mistakes that I don't know how to fix. Could someone please look at this and tell me if I am on the right track or not. I don't know how to finish lines 59 and 60. My file opens up and the cities and states go into the listbow. I can select a city but then I get the error "An unhandled exception of type 'System.NullReferenceException' occurred

Additional information: Object reference not set to an instance of an object." Also when I try to close the box with this error written on it then a page opens up that is titled "on source available" why am I getting this page? When a city is selected the program is supposed to do the calcdistance for all the other cities and display the five closest cities and there distances, using the lat and lon, and display them in the labels. I am supposed to have the labels blank by default and I only have a couple of them set that way right now so that is why they look like that. Any help would be greatly appreciated. Thank you


PHP Code:


Imports System
.IO

Public Class Form1
    Dim closestCity
() As String
    
Private state(125), city(125) As String
    
Private lat(1250), lon(1251) As Double


    
Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load
        Dim sr 
As New StreamReader("cities.cvs")
        
Dim inline As String
        Dim infield
() As String
        Dim counter 
As Integer
        sr
.ReadLine()
        
counter 0
        
While Not sr.EndOfStream
            inline 
sr.ReadLine
            infield 
inline.Split(",")
            
city(counter) = infield(0)
            
state(counter) = infield(1)
            
lat(counter0) = infield(2)
            
lon(counter1) = infield(3)
            
ListBox1.Items.Add(city(counter) & ", " state(counter))
            
counter += 1
        End 
While

        
'sr.Close()
    End Sub


    Private Function DegreesToRadians(ByVal Deg As Double)
        ' 
Converts radians to numeric (signeddegrees
        
Return Deg Math.PI 180.0
    End 
Function

    Private Function 
RadiansToDegrees(ByVal Rad As Double)
        
' Converts numeric degrees to radians 
        Return Rad * 180.0 / Math.PI
    End Function

    Private Function CalcDistance(ByVal Lat1 As Double, ByVal Lon1 As Double, ByVal Lat2 As Double, ByVal Lon2 As Double) As Double
        ' 
Calculate the Great Circle Distance between two points on the Earth,
        
' based on the Latitude and Longitude of the two places
        Dim Radius As Double = 6371.0           ' 
Radius of the Earth in kilometers
        Dim DeltaLat 
As Double DegreesToRadians(Lat2 Lat1)
        
Dim DeltaLon As Double DegreesToRadians(Lon2 Lon1)
        
Lat1 DegreesToRadians(Lat1)
        
Lat2 DegreesToRadians(Lat2)
        
Dim A As Double Math.Sin(DeltaLat 2.0) * Math.Sin(DeltaLat 2.0) +
            
Math.Sin(DeltaLon 2.0) * Math.Sin(DeltaLon 2.0) * Math.Cos(Lat1) * Math.Cos(Lat2)
        
Dim C As Double 2.0 Math.Atan2(Math.Sqrt(A), Math.Sqrt(1.0 A))
        
Dim D Radius C
        
Return D
    End 
Function
    Private 
Sub ListBox1_SelectedIndexChanged(sender As System.ObjectAs System.EventArgsHandles ListBox1.SelectedIndexChanged
        Dim lat1
(), lat2() As String
        Dim lon1
(), lon2() As String
        Dim distances 
As String
        Dim closestCity
() As Integer CalcDistance(lat1(ListBox1.SelectedIndex), lon1(ListBox1.SelectedIndex), lat2(""), lon2(""), to array()
        
Dim distances() As Double = Array.ConvertAll(closestCity.Take(4).ToArray, Function(xCalcDistance(lat(ListBox1.SelectedIndex), lon(ListBox1.SelectedIndex), lat(x), lon(x))) as double
        Label4
.Text city(closestCity(1)) : Label5.Text distances(1).ToString
        Label6
.Text city(closestCity(2)) : Label7.Text distances(2).ToString
        Label8
.Text city(closestCity(3)) : Label9.Text distances(3).ToString
        Label10
.Text city(closestCity(4)) : Label11.Text distances(4).ToString
        Label12
.Text city(closestCity(5)) : Label13.Text distances(5).ToString

    End Sub

    
Private Sub Button1_Click(sender As System.ObjectAs System.EventArgsHandles Button1.Click
        Me
.Close()
    
End Sub
End 
Class 


Viewing all articles
Browse latest Browse all 27419

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>