Results 1 to 11 of 11

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

  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.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Help with turning this sub into a function

    What is the function's return value? I'm not sure why you need an object clause in there, but you can pass arguments to this function by giving it an arguments list. For example,

    txtMiles.Text

    doesn't need to be in the function, it can be passed as an argument.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with turning this sub into a function

    I am going to be sending the Zip Code and a Radius from the form to the function. The function will return a DataSet with the City, State and Zip (the Select statement near the end of this code block).

    I think it is looking for something like this:

    Function GetRadius() As String

    But since I am going to end up sending a dataset I don't know if a string is the correct object.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Help with turning this sub into a function

    So if it returns a dataset,

    Function GetRadius() As DataSet


    The last line of your function's code should be

    Return ds


    Do not bind your datagrid to the dataset in the function itself. Bind it to the dataset RETURNED by your function.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with turning this sub into a function

    Next these two variables have to become Public Variables. This is how I would normally handle doing this. Is that an OK way of doing it?

    Public iRadius = CInt(txtMiles.Text)
    Public strZip As String = CStr(txtZip.Text)

    It would seem there should be a way to do this right from the calling sub but if there is I cannot get the syntax correct. Is it possible do pass these values along to the function from here?

    Dim strZip As String = (tbZip.Text)
    Dim iRadius As Integer = (tbRadius.Text)
    GetRadius()

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with turning this sub into a function

    I don't have this last part figured out and it probably doesn’t matter because I have something that works. I'll worry about "Best Practices" as I progress though my VB learning experience.

    I did figure out the rest of the Returning of the Dataset part and will post the solution here hoping someone finds it useful.

    The function will end with something like this
    VB Code:
    1. Dim Cmd2 As New OleDbDataAdapter(strSelect2, conn)
    2.         Dim ds As New DataSet
    3.         Cmd2.Fill(ds, "ZipCodes")
    4.  
    5.         Return ds
    6.  
    7.     End Function
    And the Sub to fill the datagrid should look something like this
    VB Code:
    1. dgZipCodes.DataSource = GetZipRadius().Tables("ZipCodes")

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help with turning this sub into a function

    *whack!* That's the sound of me slapping you on the wrists.... do NOT use a function like that..... everytime you do it like that, it's a call to the db and the dataset is thrown away at the end of the line....
    It would be MUCH better to set a DataSet variable to the function and then bind THAT to the grid:

    VB Code:
    1. Dim myDS as DataSet = GetZipRadius()
    2.  
    3. dgZipCodes.DataSource = myDS

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with turning this sub into a function

    Ow that hurt! But thanks for the tip anyway! Is the difference that in your version the dataset is held in memory for as long as the form is open?

    How do I ensure the form opens with the table filled and not as an undelined link as it is doing now that I removed the ds.Tables("ZipCodes")
    Last edited by FastEddie; Mar 1st, 2006 at 05:13 PM.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help with turning this sub into a function

    To answer your first question, if it's a module level variable, then yes.

    As for the other part... I usualy fill my datasets in the constructor of the form, right after the call to the InitializeComponents..... seems to work for me.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Help with turning this sub into a function

    You should be using option strict and option explicit too. Stops you writing bad code.
    I don't live here any more.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with turning this sub into a function

    How do I ensure the form opens with the table filled and not as an undelined link as it is doing now that I removed the ds.Tables("ZipCodes")
    I should update this thread. I found the answer to my question. The key is to add one more line and that is the DataMember like so.
    VB Code:
    1. Dim myDS As DataSet = GetUE_ZipRadius(tbZip.Text, CInt(tbRadius.Text))
    2. DataGrid1.DataSource = myDS
    3. DataGrid1.DataMember = "ZipCodes"

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