Results 1 to 11 of 11

Thread: Help with turning this sub into a function (Resolved)

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Help with turning this sub into a function (Resolved)

    I am trying to step up my learning of OOP and one of the things I am doing to get the hang of it is going through the process of compartmentalizing my long subs into smaller functions. I have been moderately successful at doing this but have had a lot of trouble and have, in many cases, resorting to trial and error (guesing) until I get it right. This morning I ran into a roadblock and I want to walk through this a step at a time so it that it will make more sense.

    The first thing the IDE wants me to do is set the Object Clause for the Function. Is that where I should have iRadius as Int and strZip as Text?
    VB Code:
    1. Function GetRadius()
    2.  
    3.         iRadius = CInt(txtMiles.Text)
    4.         Dim strZip As String = CStr(txtZip.Text)
    5.  
    6.         Dim conn As OleDbConnection = New OleDbConnection(strCon)
    7.         Dim strSelect1 As String = ( _
    8.             "SELECT top 1 latitude, longitude, zip FROM tblZipCode " & _
    9.             "WHERE zip = @Zip")
    10.         Dim cmd1 As OleDbCommand = New OleDbCommand(strSelect1, conn)
    11.         Dim dtrZip As OleDbDataReader
    12.  
    13.         cmd1.Parameters.AddWithValue("@Zip", strZip)
    14.  
    15.         Try
    16.             conn.Open()
    17.             dtrZip = cmd1.ExecuteReader(CommandBehavior.SingleRow)
    18.             If dtrZip.Read Then
    19.                 strLat1 = CStr(dtrZip("Latitude"))
    20.                 strLon1 = CStr(dtrZip("Longitude"))
    21.                 strZip = CStr(dtrZip("Zip"))
    22.             End If
    23.             dtrZip.Close()
    24.         Catch ex As Exception
    25.             MessageBox.Show(ex.StackTrace)
    26.         Finally
    27.             conn.Close()
    28.         End Try
    29.  
    30.         Dim iStartLat As Double = CSng(strLat1)
    31.         Dim iStartLon As Double = CSng(strLon1)
    32.         Dim ZipId As String = CStr(strZip)
    33.         Dim LatRange As Double = iRadius / ((6076 / 5280) * 60)
    34.         Dim LonRange As Double = iRadius / (((System.Math.Cos(CDbl _
    35.             (iStartLat * 3.141592653589 / 180)) * 6076) / 5280) * 60)
    36.         Dim LowLatitude As Double = iStartLat - LatRange
    37.         Dim HighLatitude As Double = iStartLat + LatRange
    38.         Dim LowLongitude As Double = iStartLon - LonRange
    39.         Dim HighLongitude As Double = iStartLon + LonRange
    40.  
    41.         Dim strSelect2 As String = _
    42.             "Select Zip, City, State, AreaCode, " & _
    43.             "Latitude, Longitude From tblZipCode " & _
    44.             "Where Latitude <=" & HighLatitude & _
    45.             "And Latitude >=" & LowLatitude & _
    46.             "And Longitude <=" & HighLongitude & _
    47.             "And Longitude >=" & LowLongitude
    48.  
    49.         Dim Cmd2 As New OleDbDataAdapter(strSelect2, conn)
    50.         Dim ds As New DataSet
    51.         Cmd2.Fill(ds, "ZipCodes")
    52.         dgZipCodes.DataSource = ds.Tables("ZipCodes")
    53.  
    54.     End Function
    Last edited by FastEddie; Mar 25th, 2006 at 11:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width